user1260928 user1260928 - 1 month ago 6
AngularJS Question

Angularjs : get the index of selected object in select tag

I am trying to get the index of the selected item of my like this :

<select class="form-control" id="field_Mois" name="mois"
ng-model="vm.currentMois"
ng-options="mois as mois.libelle for mois in vm.allMoisForAnnee track by mois.id"
ng-change= "vm.moisChanged(vm.currentMois)">
</select>

vm.moisChanged = function(mois){
var i = vm.allMoisForAnnee.indexOf(mois);
}


But I am always getting -1. I don't understand why.

If someone can help me on this...

Answer

Try This

vm.moisChanged = function(mois){
    var i = vm.allMoisForAnnee.map(function(obj, index) {
        if(obj.libelle == mois.libelle) {
            return index;
        }
    }).filter(isFinite);
}
Comments