2015-06-20 70 views
1

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

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

回答

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

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"> 
相关问题