更改数据后,指令无效

时间:2015-02-19 06:54:21

标签: angularjs angularjs-directive

我已经停止使用指令,禁用表单元素。

app.directive('chDisable', function()  {
        return {
            link: function (scope, element, attrs) {
                debugger;
                var disableListName = attrs.chDisable;
                var disableList = scope[disableListName];

                $(element).find('*[ng-model]').each(function (index, item) {
                    var model = $(item).attr('ng-model');

                    for (var i = 0; i < disableList.length; i++) {
                        if (model.indexOf(disableList[i]) > -1) {
                            $(item).attr('disabled', 'disabled');
                        }
                    }
                });
            }
        };
    });

它使用arrayList来声明禁用元素。

$scope.disableItemList = ['PropertyType'];

如果指令在arraylist中找到children元素,那么它将禁用元素。      当arreylist在initilizing之后填充了items或arrayList时它会工作。 问题是当我在某些事件中更改arraylist项目时(或者例如通过浏览器控制台),指令不会触发并且不会禁用表单元素。

    <div ch-disable="disableItemList">
    <input ng-model="aaa"/>
    <input ng-model="bbb" />
    <input type="checkbox" ng-model="ccc" />
</div>

1 个答案:

答案 0 :(得分:1)

https://docs.angularjs.org/api/ng/type/$rootScope.Scope

您需要在$watch()函数中使用$watchCollection()link,以便在列表更改时刷新元素。