admin管理员组文章数量:1434949
I have created select options with ng-repeat, but the function set with on-change is never called.
<select>
<option ng-model="level1Selected" ng-repeat="item1 in level1" ng-change="setLevel2()" value="{{item1}}">{{item1}}</option>
I recreated the same thing with ng-options, and the function gets called:
<select ng-options="item1 for item1 in level1" ng-model="level1Selected" ng-change="setLevel2()"></select>
I have checked the documentation, and I see no reason for this difference. The variable level1 is an array of strings, so I don't understand why they behave differently.
The function is currently just a placeholder with console.log:
$scope.setLevel2 = function() {
console.log("value: ");
}
I have created select options with ng-repeat, but the function set with on-change is never called.
<select>
<option ng-model="level1Selected" ng-repeat="item1 in level1" ng-change="setLevel2()" value="{{item1}}">{{item1}}</option>
I recreated the same thing with ng-options, and the function gets called:
<select ng-options="item1 for item1 in level1" ng-model="level1Selected" ng-change="setLevel2()"></select>
I have checked the documentation, and I see no reason for this difference. The variable level1 is an array of strings, so I don't understand why they behave differently.
The function is currently just a placeholder with console.log:
$scope.setLevel2 = function() {
console.log("value: ");
}
Share
Improve this question
asked Nov 23, 2016 at 15:03
balaganbalagan
1461 silver badge5 bronze badges
2
-
1
put
ng-change
on theselect
element notoption
since it isselect
's value that changes – mic4ael Commented Nov 23, 2016 at 15:06 -
1
ng-change
would work onselect
/input
, it isn't intended(never) work onoption
tag – Pankaj Parkar Commented Nov 23, 2016 at 15:06
2 Answers
Reset to default 4If you want to do that you need to put ng-model and ng-change in select tag, try this:
<select ng-model="level1Selected" ng-change="setLevel2()">
<option ng-repeat="item1 in level1" value="{{item1}}">{{item1}}</option>
</select>
You need trigger ng-change
directive to select
element.
The ng-change
event is triggered at every change in the value.
Here is a working solution: jsfiddle
<select ng-model="level1Selected" ng-change="setLevel2()">
<option value="{{item1}}">{{item1}}</option>
</select>
JS
$scope.setLevel2 = function() {
alert();
}
本文标签: javascriptngchange does not work with ngrepeat with options but works with ngoptionsStack Overflow
版权声明:本文标题:javascript - ng-change does not work with ng-repeat with options but works with ng-options - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745635332a2667522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论