更改数据后指令不起作用
Directive is not working after changing data
我已经设置了使用后禁用表单元素的指令。
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 中找到子元素,则它会禁用元素。
它在 arreylist 在初始化后填充了项目或 arrayList 时工作。
问题是当我在某些事件中更改 arraylist 项时(或者例如通过浏览器控制台)指令不会触发并且不会禁用表单元素。
<div ch-disable="disableItemList">
<input ng-model="aaa"/>
<input ng-model="bbb" />
<input type="checkbox" ng-model="ccc" />
</div>
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
您需要在 link
函数中使用 $watch()
或 $watchCollection()
以便在列表更改时刷新元素。
我已经设置了使用后禁用表单元素的指令。
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 中找到子元素,则它会禁用元素。 它在 arreylist 在初始化后填充了项目或 arrayList 时工作。 问题是当我在某些事件中更改 arraylist 项时(或者例如通过浏览器控制台)指令不会触发并且不会禁用表单元素。
<div ch-disable="disableItemList">
<input ng-model="aaa"/>
<input ng-model="bbb" />
<input type="checkbox" ng-model="ccc" />
</div>
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
您需要在 link
函数中使用 $watch()
或 $watchCollection()
以便在列表更改时刷新元素。