0
我正在使用指令來構建一個自定義驗證器,它工作正常。但是,它只被稱爲一次!如果我的「roleItems」被更新,這個指令不會再被調用!每次更新「roleItems」時如何調用它?角度指令只被調用一次
這裏是標記。而「非空」是我的指令。
<form name="projectEditor">
<ul name="roles" ng-model="project.roleItems" not-empty>
<li ng-repeat="role in project.roleItems"><span>{{role.label}}</span> </li>
<span ng-show="projectEditor.roles.$error.notEmpty">At least one role!</span>
</ul>
</form>
這是我的指令。它應該檢查ng-model「roleItems」是否爲空。驗證的
angular.module("myApp", []).
directive('notEmpty', function() {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$validators.notEmpty = function (modelValue, viewValue) {
if(!modelValue.length){
return false;
}
return true;
};
}
};
});
你打算在ul上做什麼? ng-model用於對輸入等組件進行雙向數據綁定,其中用戶可以輸入存儲在模型變量中的值。 ul不能這樣做。這沒有意義。只需刪除此指令並使用 –