How do I set ng-option model to the value of an object?

109 views Asked by At

I'm pulling in an array of objects as $scope.templates. I'd like to be able to set $scope.item.steps (my select model name) to the value of template.steps for the selected option in my ng-options array. Is there an easy way to do this without having add extra controller logic?

<select class="form-control" ng-options="template.name for template in templates" ng-model="item.steps">
2

There are 2 answers

0
Carson Drake On BEST ANSWER

Beyers answered it spot on. Just one suggestion would be to use track by

<select class="form-control" ng-options="template.steps as template.name for template in templates track by template.id" ng-model="item.steps">  

It doesn't need to be template.id but you should track some unique identifier. This helps with a number of performance/rendering issues.

Definitely necessary if:

  • you are going to be updating item.step outside of this input
  • you have a dynamic options collection (in this case templates)

Related: ng-repeat , ng-options

0
Beyers On

Sure, use the select as label for value in array syntax:

<select class="form-control" ng-options="template.steps as template.name for template in templates" ng-model="item.steps">