0
<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.

4 Answers 4

1
<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

Sign up to request clarification or add additional context in comments.

Comments

0

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>

2 Comments

to be more angular specific it should be <option ng-repeat = "x in state" ng-value="x.code"> {{x.name}} </option>
Thanks for the solution
0

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.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.