Javascript 如何在自定义指令中将字段设置为$dirty?
我有一个下拉/选择指令,用于替换表单中的选择框。我有几个按钮,我设置为禁用,只要形式是原始的Javascript 如何在自定义指令中将字段设置为$dirty?,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个下拉/选择指令,用于替换表单中的选择框。我有几个按钮,我设置为禁用,只要形式是原始的 app.directive('dropdown', function ($timeout) { return { restrict: 'E', template: '<div class="btn-group" dropdown>' + '<button id="dropdownDirective" class="btn
app.directive('dropdown', function ($timeout) {
return {
restrict: 'E',
template: '<div class="btn-group" dropdown>' +
'<button id="dropdownDirective" class="btn dropdown-toggle" dropdown-toggle>' +
'{{ items[ngModel].name }}' +
'<span class="caret"></span>' +
'</button>' +
'<ul class="dropdown-menu" role ="menu" aria-label="dLabel">' +
'<li ng-repeat="item in items">'+
'<a href="#" ng-bind="item.name" ng-click = "select(item)" >< / a >' +
'</li>'+
'</ul>'+
'</div>',
scope: {
ngModel: '=', // selected option
items: '=', // options
},
link: function (scope, element, attrs) {
scope.select = function (item) {
scope.ngModel = item.id;
};
}
};
});
在HTML中:
<dropdown id="modalSelect"
ng-model="ahs.modal.modalId"
items="es.modal.data">
</dropdown>
根据我的指令,$pristine不会被触发。有人能指出我做错了什么吗?您需要在指令中实现整个表单相关行为。与以下内容类似的东西:
app.directive('dropdown', function ($timeout) {
return {
require: "ngModel",
restrict: 'E',
template: [...]
scope: {
items: '=', // options
},
link: function (scope, element, attrs, ngModelController) {
ngModelController.$setViewValue(...);
ngModelController.$setPristine(...);
etc.
}
};
});