【问题标题】:button not enabling after changing value from angularJS directive从 angularJS 指令更改值后按钮未启用
【发布时间】:2013-12-26 05:02:15
【问题描述】:

基本上,我得到了一个带有按钮和可选列表 (jquery-UI) 的 AngularJS 指令。

使用 ng-disabled="disabledButton" 禁用按钮。

disabledButton是控制器$scope.disabledButton = true;中的一个变量

现在,在指令中,单击列表中的元素后,我想将 disabledButton 设置为 false,以便激活按钮。

这是我的 HTML 代码:

<div class="modal-wrapper">
    <div panels test-data="testData" disable-button='disableButton'></div>

    <div class="bottom-buttons">
        <button ng-disabled="disableButton" class="btn btn-primary btn-wizard" ng-click="alertSelected()">NEXT</button>
    </div>
</div>

还有我的指令代码:

portfolioApp.directive('panels', function($timeout) {
    return {
        restrict: 'A',
        replace: true,
        templateUrl: 'templates/paneltemplate.htm',
        scope: {
            testData: "=",
            disableButton: "@"
            //selectedList: "@"
        },
        controller: function($scope) {
            $scope.selectedList = [];

            $scope.clearList = function() {
                $scope.selectedList.length = 0;
            }
        },
        link: function($scope, element, attrs, panelsController) {
            $timeout(function() {
                if(!$scope.$$phase) {
                    $('#selectable').selectable( {
                        filter: "li",
                        selected: function(event, elem) {

                            // this will return the first of the selecteds
                            var selecteds = $('#selectable').find('.ui-selected').children().eq(0).children().text();

                            $scope.selectedList.push(selecteds);
                            console.log($scope.selectedList);

                            $scope.$apply(function() {
                                $scope.disableButton = false;
                            });

                            console.log('look im being selected!')
                        },
                        unselected: function(event, ui) {
                            $scope.clearList();
                        }
                    }); 
                    $scope.$apply();
                }
            },0);           
        }
    }
})

【问题讨论】:

  • 用 $scope.$apply 替换 $scope.$watch

标签: javascript jquery jquery-ui angularjs


【解决方案1】:
link: function(scope, element, attrs) {
        $timeout(function() {
            if(!scope.$$phase) {
                $('#selectable').selectable( {
                    filter: "li",
                    selected: function(event, elem) {

                        scope.$apply(function() {
                            scope.disableButton = false;
                        });
                        console.log('look im being selected!')
                    }
                }); 
            }
        },0);           
    }

【讨论】:

  • 您的代码什么都不做,因为 $scope.$watch 只监控范围内的值以进行更改。
  • 我在我的代码中添加了另一件事,我假装也这样做,将选定对象的数组返回给控制器...我试图在指令中使用内部控制器,但我无法从父控制器看到它,也许我应该添加一个 $broadcast/$emit?会是个好主意吗?
  • 如果可以,请创建小提琴。可能你可以用 ng-click='enableButton();' 重写你的代码在每个 li 和控制器中 scope.enableButton = function(){ scope.disableButton = false; }
  • 这样可以删除链接功能
  • 嗯,不,我不能:(难道没有办法从指令中做到这一点吗?:S
猜你喜欢
  • 2016-12-13
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 2015-07-13
  • 2012-12-29
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多