我在AngularJS中使用了一個指令,我試圖檢查我的字段是否是$ dirty,如果這是真的我想記錄一條消息,當它發生變化時我想顯示另一條消息(這句話稍後將被替換爲更復雜的東西)。Angular指令不看
我想在所有的輸入中重用我的指令,但我不能。提前致謝。
<input ng-edited ng-model="stakeholder.nombre" name="nombre" type="text">
我的指示是:
MetronicApp.directive('ngEdited', function() {
return {
restrict: 'A',
require: ['^form', 'ngModel'],
link: function (scope, elem, attrs, req) {
scope.elemDirty = req[1].$dirty;
scope.elemName = req[1].$name;
var doFunction = function() {
if (scope.elemDirty) {
console.log("true");
} else {
console.log("false");
}
};
scope.$watch(scope.elemName, doFunction, true);
}
};
});
我不確定在這個例子中'req [1]'是什麼。通常一個控制器作爲第四個參數傳遞給鏈接函數。 – Claies
@Claies我正在發送第4個arg中的兩個項目:form和ngmodel,你可以在require中看到它 –