Javascript 如何为select list的自定义指令实现ng更改?
我的指令使用代码Javascript 如何为select list的自定义指令实现ng更改?,javascript,angularjs,angularjs-directive,angularjs-scope,ng-options,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,Ng Options,我的指令使用代码 <input-select ng-model="someModel" ng-change="someFunction()" options="countries"></input-select> 我的指令模板 <select ng-model="ngModel" ng-init="ngModel " ng-options="option[optionId] as opt
<input-select ng-model="someModel" ng-change="someFunction()"
options="countries"></input-select>
我的指令模板
<select
ng-model="ngModel" ng-init="ngModel "
ng-options="option[optionId] as option[optionName] for option in options"
ng-change="ngChange">
</select>
因此,当所选项被更改时,函数someFunction()
将被无限次调用(尽管更改只执行了一次),为了确保someFunction()
get只调用一次(someFunction()),应该更改什么
是使用指令的控制器范围内的函数)
[我确实尝试过使用
&
和@
作为ngChange的作用域类型,somefunction()
如果使用它们,不会被解雇。]你应该使用&
用于表达式,从标记中你可以调用类似ngChange()
的方法,而不仅仅是ngChange
标记
<select
ng-model="ngModel" ng-change="ngChange()"
ng-options="option[optionId] as option[optionName] for option in options">
</select>
&
或@
不起作用-某些函数(
如果使用,则无法执行。@VishwajeetVatharkar尝试我建议的方法..按我所说的更改您的代码..在那之前,我将为您创建一个plunkr使其ngChange()
在模板中也不起作用。@VishwajeetVatharkar你能看看我在答案中添加的plunkr吗..你不应该担心ng change
事情,它应该是ng change=“someFunction(someModel)”
而不是ng change=“someFunction(someOtherModel)”
,因为最终它只是一个参数名,不管它是什么,都会提供给控制器。。结帐
<select
ng-model="ngModel" ng-change="ngChange()"
ng-options="option[optionId] as option[optionName] for option in options">
</select>
scope: {
ngModel: '=',
ngChange: '&'
}