javascript - ng-change does not work with ng-repeat with options but works with ng-options - Stack Overflow

I have created select options with ng-repeat, but the function set with on-change is never called. <

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 the select element not option since it is select's value that changes – mic4ael Commented Nov 23, 2016 at 15:06
  • 1 ng-change would work on select/input, it isn't intended(never) work on option tag – Pankaj Parkar Commented Nov 23, 2016 at 15:06
Add a ment  | 

2 Answers 2

Reset to default 4

If 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();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745635332a4637357.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信