通常の入力を使用して、など
<form name="myForm">
<input type="text" ng-model="foobar">
</form>
入力ボックスに入力した後myForm.$dirty
はtrueです。
私は使用例が
<form name="myForm">
<my-directive foo-bar="myObj.foobarValue"></my-directive>
</form>
と2つのボタンのいずれかをユーザーがクリックした後
は、myForm$dirty
がtrueに設定されているだろうな
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">
<input type="text" ng-model="foobar">
<my-directive ng-model="foobar"></my-directive>
</form>
:
angular.module('myModule', [])
.directive('myDirective', function() {
return {
restrict: 'E',
require: { ngModelCtrl: 'ngModel' },
scope: {
ngModel: '<'
},
bindToController: true,
controllerAs: '$ctrl',
template:
`<div>
<button ng-click="$ctrl.ngModelCtrl.$setViewValue('foo')">
Set foo
</button>
<button ng-click="$ctrl.ngModelCtrl.$setViewValue('bar')">
Set bar
</button>
</div>`,
controller: function ctrl() {}
};
});
使用:
を参照してください、それは受け入れられるだろうか? – user2718281
[ngFormController API - $ setDirty](https://docs.angularjs.org/api/ng/type/form.FormController#$setDirty)を使用します。 – georgeawg