當使用常規的輸入,如
<form name="myForm">
<input type="text" ng-model="foobar">
</form>
在輸入框中鍵入後myForm.$dirty
是真實的。
我想創建一個簡單的指令如
angular.module('myModule', [])
.directive('myDirective', function() {
return {
restrict: 'E',
scope: {
fooBar: '='
},
template: '<div><button ng-click="fooBar=foo"></button><button ng-click="fooBar=bar"></button></div>'
};
});
樣品的用法是
<form name="myForm">
<my-directive foo-bar="myObj.foobarValue"></my-directive>
</form>
,並在任何兩個按鈕的用戶點擊後,myForm$dirty
設置爲true。
這是如何完成的?
如果該指令定義每個按鈕採取行動,而不是在模板中的任何按鈕時,它會更容易些,那可以接受嗎? – user2718281
使用[ngFormController API - $ setDirty](https://docs.angularjs.org/api/ng/type/form.FormController#$setDirty) – georgeawg