如何将ng-option模型设置为对象的值?

时间:2015-06-20 21:35:01

标签: javascript angularjs ng-options

我正在将对象数组作为$scope.templates。我希望能够将$scope.item.steps(我的选择型号名称)设置为ng-options数组中所选选项的值template.steps。没有添加额外的控制器逻辑,有没有一种简单的方法可以做到这一点?

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

2 个答案:

答案 0 :(得分:2)

当然,请使用select as label for value in array语法:

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

答案 1 :(得分:1)

Beyers现场回答。只有一个建议是使用 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">  

它不需要为template.id,但您应该跟踪一些唯一标识符。这有助于解决许多性能/渲染问题。

绝对必要,如果:

  • 您将在此输入之外更新item.step
  • 您有动态选项集(在本例中为templates

相关:ng-repeatng-options

相关问题