【问题标题】:AngularJs accordion not opening with is-open attributeAngularJs 手风琴不使用 is-open 属性打开
【发布时间】:2016-12-09 11:43:33
【问题描述】:

我遇到了一个奇怪的问题。我写了我自己的指令。我正在尝试使用

开放

根据名为$scope.state 的模型指定何时需要打开手风琴组的属性。不幸的是,它的属性 isOpen 始终为 false,即使它在 checked() 函数中更改为 true。

Directive.html

<accordion>
    <accordion-group class="my-panel-body" ng-class="{'my-panel-heading-checked':state.isOpen,'my-panel-heading-default':!state.isOpen}" is-open="state.isOpen">
         <!--Accordion header -->
         <accordion-heading id="{{groupId}}" >
             <div ng-click="checked()" >
                <i class="pull-left glyphicon" ng-class="status"></i> {{title}}
             </div>
        </accordion-heading>

        <!--Accordion content -->
    </accordion-group>
</accordion>

Directive.js

angular.module('locale').
    directive('accordionList', function () {
        return{
            restrict: 'A',
            scope: {
                title: '=',
                result: '=',
                groupId: '='
            },
            templateUrl: 'html/accordionList.html',
            controller: function($scope){

            var glyphicon = 'glyphicon glyphicon-';
            var status = glyphicon + 'unchecked';
            $scope.state = {
                isOpen: false
            }
            $scope.status = status;

            $scope.checked =  function(){
                console.log($scope.state.isOpen) //It is always false!
                if ($scope.state.isOpen) {
                    $scope.state.isOpen = false;
                    $scope.status = status;
                }
                else {   //Always entering this part of code!
                    console.log("FLAG");
                    $scope.state.isOpen = true; //True only here when invoked.
                    $scope.status = glyphicon + 'check';
                    console.log($scope.state.isOpen);  

                }
            }          
        }
   };

});

如果有任何帮助,我将不胜感激。我不知道什么不起作用。

【问题讨论】:

  • 你可以为它做一个 plunker 或 fiddle 吗?

标签: angularjs data-binding accordion collapse angular-ngmodel


【解决方案1】:

根据您提供的代码,我创建了一个plunker 来重现您的问题,它确实显示了问题。

事实上,accordion-group 指令会自动将toggleOpen 函数绑定到点击事件(参见template)。在这种情况下,使用您自己的 checked 函数,您的 isOpen 值会在元素被单击时更改两次,因此保持不变。

所以只需消除 'check()' 或停止更改其中的 isOpen 值。

$scope.checked =  function(){

                console.log($scope.state.isOpen) //It is always false!
                if ($scope.state.isOpen) {
                    //$scope.state.isOpen = false;
                    $scope.status = status;
                }
                else {   //Always entering this part of code!
                    console.log("FLAG");
                  //  $scope.state.isOpen = true; //True only here when invoked.
                    $scope.status = glyphicon + 'check';
                    console.log($scope.state.isOpen);  

                }
            }

【讨论】:

  • 感谢您的快速回复。我明天会检查它是否能解决问题。
  • 嗯,它有帮助,但现在我不知道如何调用函数,例如当我的手风琴崩溃时。当我单击它关闭它时,$scope.state.isOpen 在checked() 函数中为真——当它关闭时。但是当 $scope.state.isOpen 已经为 false 时,我想做一些事情。(在折叠并通过检查的 func 之后。我怎么能做到这一点>
猜你喜欢
  • 1970-01-01
  • 2015-09-21
  • 2019-07-23
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多