Lorraine
Lorraine

Reputation: 486

How I take the name in the selected option

I want show the name in a select input form when I select one of their options. I create the select like that:

<select ng-model="electionEventId" ng-options="option.value as option.name for option in electionEvents">
</select>

And I catch the selected item value with {{ electionEventId }} but I want the name too of this election without realize another request. ¿Anyone can help me?

Thanks

Upvotes: 1

Views: 47

Answers (2)

gustavohenke
gustavohenke

Reputation: 41440

My answer seems to be a little too verbose compared to other answers here, but I hope it helps!

I believe that you will have to traverse the electionEvents looking for the option that has value property equal to electionEventId.

For example, add this function to your controller:

$scope.getElectionEventName = function() {
  var name;
  angular.forEach( $scope.electionEvents, function( option ) {
    // Don't process if the name has already been found
    name != null && return;

    if ( option.value === $scope.electionEventId ) {
      name = option.name;
    }
  });

  return name;
};

I haven't tested it, but I'm pretty sure it works!
Unfortunately there's no how to break the loop, so I put that line in the loop function :)

Upvotes: 0

D.Evangelista
D.Evangelista

Reputation: 6543

You can bind the entire object in your model:

<select ng-model="electionEvent" ng-options="option as option.name for option in electionEvents">
</select>

Upvotes: 3

Related Questions