balagan balagan - 1 year ago 62
AngularJS Question

ng-change does not work with ng-repeat with options but works with ng-options

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

<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: ");

Answer Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download