2014-02-27 77 views
1
<ul ng-repeat="books in companyBookData.Bookparts" style="float: left"> 
    <li> 
     <select 
      name="Zone" 
      id="Zone" 
      ng-options=" ZoneType.Description for ZoneType in books.ZoneTypes" 
      ng-model="ZoneType" 
      ng-init="ZoneType=books.ZoneTypes[0]"> 
     </select> 
    </li> 
<ul> 

我有下拉菜單,它使用ng-repeat生成,如上所示。我將至少有4個下拉列表和最多100個下拉列表,並且我明白ng-repeat會在每次迭代中創建一個新的範圍。我如何設置每個下拉菜單的默認值?我可以訪問由ng-repeat生成的所有範圍嗎?我如何設置默認值?使用ng-repeat設置下拉菜單的默認設置

+1

嘗試像ul ng-repeat =「booksBookData.Bookparts」中的ng-controller =「MyCtrl」,您可以參考每本書 – Whisher

+0

@Whisher非常感謝!對我來說工作得很好。 – GeekBoy

回答

0
<ul ng-repeat="books in companyBookData.Bookparts" style="float: left"> 
<li> 
    <select 
     name="Zone" 
     id="Zone" 
     ng-options=" ZoneType.Description for ZoneType in books.ZoneTypes" 
     ng-model="ZoneType" 
     ng-init="ZoneType=books.ZoneTypes[0]" 
     ng-controller="ZoneTypeController" 
     > 
    </select> 
</li> 

如Whisher建議我加入了NG-控制器指令並設置默認那裏。 companyBookData.Bookparts的每次迭代都會調用此控制器。

0

試試這個

ng-init="ZoneType= ZoneType || books.ZoneTypes[0]"> 
0

您不能分配的NG-模型和NG選項相同的屬性。您可以像使用ng-init一樣僅使用定義一個新變量來存儲選定對象。

<ul ng-repeat="books in companyBookData.Bookparts" style="float: left"> 
    <li ng-init="selectedZoneType=books.ZoneTypes[0]"> 
     <select 
      name="Zone" 
      ng-options=" ZoneType.Description for ZoneType in books.ZoneTypes" 
      ng-model="selectedZoneType"> 
     </select> 
    </li> 
<ul> 

不要在ng-repeat中定義ids,否則你會在html中得到重複的id。除非你使用$ index來使它們唯一。