我有一个dropdown / select指令,用于替换表单中的选择框。只要表格是原始的,我就设置了几个禁用的按钮。
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没有被触发。有人能指出我做错了吗?
答案 0 :(得分:0)
您需要在指令中实现与表单相关的整个行为。以下内容:
https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
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.
}
};
});