balagan
balagan

Reputation: 146

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.

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

Upvotes: 0

Views: 4759

Answers (2)

Donnie
Donnie

Reputation: 94

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>

Upvotes: 4

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48367

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

Upvotes: 1

Related Questions