Angularjs 为使用ng repeat生成的下拉列表设置默认值
我有使用ng repeat生成的下拉列表,如上所示。我将有至少4个下拉列表,最多100个,我知道ng repeat在每次迭代中都会创建一个新的范围。如何设置每个下拉列表的默认值?我是否可以访问ng repeat生成的所有作用域?如何设置默认值?试试这个Angularjs 为使用ng repeat生成的下拉列表设置默认值,angularjs,angularjs-scope,angularjs-ng-repeat,Angularjs,Angularjs Scope,Angularjs Ng Repeat,我有使用ng repeat生成的下拉列表,如上所示。我将有至少4个下拉列表,最多100个,我知道ng repeat在每次迭代中都会创建一个新的范围。如何设置每个下拉列表的默认值?我是否可以访问ng repeat生成的所有作用域?如何设置默认值?试试这个 <ul ng-repeat="books in companyBookData.Bookparts" style="float: left"> <li> <select
<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模型和ng选项指定相同的属性。您可以使用nginit,就像您刚才使用的一样,只需定义一个新变量来存储所选对象
ng-init="ZoneType= ZoneType || books.ZoneTypes[0]">
-
不要在ng repeat中定义id,否则您将在html中获得重复的id。除非您使用$index使它们唯一。
<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>
根据Whisher的建议,我添加了一个ng控制器指令,并在那里设置了默认值。对于companyBookData.Bookparts的每次迭代,都会调用此控制器。尝试类似于MyCtrl中的ul ng repeat=“books in companyBookData.Bookparts”ng controller=“MyCtrl”您对每个控制器都有一个引用books@Whisher谢谢!!!对我来说很好。
<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>