2015-06-20 34 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"> 
相關問題