我试图让自定义指令激活ngChange事件,以便原始控制器在指令中的数据发生更改时执行某些操作。从自定义指令触发ngChange
我的指令看起来像这样:
.directive('spInputCheckbox', function() {
return {
scope: {
ngModel: '=',
ngChange: '&'
},
restrict: 'AE',
replace: 'true',
template: '<div><span ng-click="model = !model"><i ng-if="model">X</i></span><input type="checkbox" ng-model="model" ng-change="change(model)" /><span ng-if="title">{{ title }}</span></div>',
link: function(scope, elem, attr){
scope.title = attr.title
scope.$watch('ngModel', function(value){
if(value){
scope.model = scope.ngModel
}
});
scope.change = function(value){
scope.ngModel = scope.model
scope.ngChange()
}
}
};
})
这里就是我称之为
<sp-input-checkbox ng-model="info"
ng-init="info = false"
title="click me"
ng-change="alert(7)" />
这里有一个JsFiddle与代码
这将得到警报显示。但不会触发我正在寻找的ng-change事件 –
是的。但由于它是一个复选框,点击是唯一可能的改变,为什么你需要ng-change事件? – pahan
万一它不是复选框,比如输入类型=“text” –