我的指令使用代码
<input-select ng-model="someModel" ng-change="someFunction()"
options="countries"></input-select>
我的指令代码
.directive('inputSelect', function () {
return {
templateUrl: 'someTemplate.html',
restrict: 'E',
scope: {
ngModel: '=',
ngChange: '='
}
};
});
我的指示模板
<select
ng-model="ngModel" ng-init="ngModel "
ng-options="option[optionId] as option[optionName] for option in options"
ng-change="ngChange">
</select>
因此,当所选项目发生更改时,函数someFunction()
将被无限次调用(尽管更改已完成一次),应该更改哪些内容以确保someFunction()
获取&# 39; s只调用一次(someFunction()
是在使用该指令的控制器范围内的函数)
[我的确尝试使用&
和@
作为ngChange的范围类型,somefunction()
如果使用这些范围,则不会被解雇。 ]
答案 0 :(得分:5)
您应该使用用于表达的&
来自标记,您可以将该方法称为ngChange()
,而不仅仅是ngChange
<强>标记强>
<select
ng-model="ngModel" ng-change="ngChange()"
ng-options="option[optionId] as option[optionName] for option in options">
</select>
<强>代码强>
scope: {
ngModel: '=',
ngChange: '&'
}