Daemon_Demon
Daemon_Demon

Reputation: 3

how to use indexof in array set in angularjs

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myctrl', function($scope) {
    $scope.state = [{name:'TamilNadu', code:1}, {name:'Kerala', code:2}, {name:'Karnataka', code:3}];
    $scope.onChange = function() {
            var a = this.drop.state;  }
});
</script>
<body>

<div ng-app="myApp" ng-controller="myctrl">
State : 
<select id="stat" ng-model="drop.state" ng-change="onChange()">
<option ng-repeat = "x in state"> {{x.name}} </option>
<select>
</div>
</body>

In this code, I can be able to get the selected value from the textbox in the variable a. Now, I need to get the code value of the name selected. Is it possible using indexof(). And I need it in dynamic ie., when the 'state' is selected I need that corresponding 'code' from the array set.

Upvotes: 0

Views: 708

Answers (4)

Joao Pedro Milhome
Joao Pedro Milhome

Reputation: 11

<option value="{{x}}" ng-repeat = "x in state"> {{x.name}} </option>

If you add the value on your Option, you may get the complete object, and accessing the code and the Value

Upvotes: 1

anoop
anoop

Reputation: 3822

You can bind data to a single property., and can access both. with preferred ng-options .

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
  var app = angular.module('myApp', []);
  app.controller('myctrl', function($scope) {
    $scope.state = [{
      name: 'TamilNadu',
      code: 1
    }, {
      name: 'Kerala',
      code: 2
    }, {
      name: 'Karnataka',
      code: 3
    }];
    $scope.onChange = function() {     
     alert(this.drop.state.name);  
     alert(this.drop.state.code);  
    }
  });
</script>

<body>

  <div ng-app="myApp" ng-controller="myctrl">
    State :
    <select id="stat" ng-model="drop.state" ng-options= "x as x.name for x in state" ng-change="onChange()"> </select>

</div>
</body>

Upvotes: 0

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

you can assign code as value to option

<option ng-repeat = "x in state" ng-value="x.code"> {{x.name}}

Demo

var app = angular.module('myApp', []);
app.controller('myctrl', function($scope) {
    $scope.drop = {}
    $scope.state = [{name:'TamilNadu', code:1}, {name:'Kerala', code:2}, {name:'Karnataka', code:3}];
    $scope.onChange = function() { 
            var a = $scope.drop.state
            console.log(a)
    }
});
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myctrl">
State : 
<select id="stat" ng-model="drop.state" ng-change="onChange()">
<option ng-repeat = "x in state" ng-value="x.code"> {{x.name}} </option>
</select>
</div>

Upvotes: 0

yBrodsky
yBrodsky

Reputation: 5041

Even though the other answer is valid, this is the right way to go about it:

<select id="stat" ng-model="drop.state" ng-options="x.code as x.name for x in state" ng-change="onChange()">
  <option value="">Select a value</option>
</select>

If the purpose of the ng-change was to get the state code, this way it's not needed. Your ng-model will be equal to the selected state code.

Upvotes: 0

Related Questions