admin 管理员组文章数量: 1087134
My directive usage code
<input-select ng-model="someModel" ng-change="someFunction()"
options="countries"></input-select>
My directive code
.directive('inputSelect', function () {
return {
templateUrl: 'someTemplate.html',
restrict: 'E',
scope: {
ngModel: '=',
ngChange: '='
}
};
});
My directive template
<select
ng-model="ngModel" ng-init="ngModel "
ng-options="option[optionId] as option[optionName] for option in options"
ng-change="ngChange">
</select>
So, when the selected item is changed, the function someFunction()
is getting called for infinite times (although the change is done once), what should be changed in order to make sure someFunction()
get's called only once ( someFunction()
is a function in the scope of the controller in which the directive is being used )
[ I did try using &
and @
for the scope type of ngChange, somefunction()
doesn't get fired if using those. ]
My directive usage code
<input-select ng-model="someModel" ng-change="someFunction()"
options="countries"></input-select>
My directive code
.directive('inputSelect', function () {
return {
templateUrl: 'someTemplate.html',
restrict: 'E',
scope: {
ngModel: '=',
ngChange: '='
}
};
});
My directive template
<select
ng-model="ngModel" ng-init="ngModel "
ng-options="option[optionId] as option[optionName] for option in options"
ng-change="ngChange">
</select>
So, when the selected item is changed, the function someFunction()
is getting called for infinite times (although the change is done once), what should be changed in order to make sure someFunction()
get's called only once ( someFunction()
is a function in the scope of the controller in which the directive is being used )
[ I did try using &
and @
for the scope type of ngChange, somefunction()
doesn't get fired if using those. ]
1 Answer
Reset to default 7You should use &
which is used for expression and from markup you could call that method like ngChange()
instead of ngChange
only
Markup
<select
ng-model="ngModel" ng-change="ngChange()"
ng-options="option[optionId] as option[optionName] for option in options">
</select>
Code
scope: {
ngModel: '=',
ngChange: '&'
}
Example Plunkr
本文标签: javascriptHow to implement ngchange for custom directive for select listStack Overflow
版权声明:本文标题:javascript - How to implement ng-change for custom directive for select list? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744044005a2523781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论