Javascript 角度选择内ng重复
我有这样的情况:Javascript 角度选择内ng重复,javascript,angularjs,Javascript,Angularjs,我有这样的情况: <span ng-repeat="personNum in unit.PricePerson"> {{personNum}} <select ng-model="personNum" ng-options="o as o for o in unit.PricePerson track by $index"></select> </span> {{personNum}}
<span ng-repeat="personNum in unit.PricePerson">
{{personNum}}
<select ng-model="personNum"
ng-options="o as o for o in unit.PricePerson track by $index"></select>
</span>
{{personNum}}
unit.Price
是数字数组,类似于[5,6,7,8,9],但每个选择的值都是9。我想先选择选项5,另一个选项6,等等。我注意到我不能用鼠标改变选项
生成了5个选择框,很好,每个选择框内还生成了5到9个选项。这些都没问题。尝试在选择中使用不同的ng模型,而不是在ng中重复并启动它以获得所需的值:
<span ng-repeat="personNum in unit.PricePerson">
{{personNum}}
<select ng-init="personNum1 = personNum" ng-model="personNum1"
ng-options="o as o for o in unit.PricePerson track by $index"></select>
</span>
然后在您的视图中使用它:
<select ng-model="selectedOptions[$index]"
ng-options="o as o for o in unit.PricePerson track by $index">
您的部分问题可能是选择中的“按$index跟踪…”。。。看一看下面的扑通一声。另外,使用ng init设置默认值 守则:
<span ng-repeat="personNum in unit.PricePerson">
<select ng-init="select[$index]=personNum" ng-model="select[$index]" ng-options="o as o for o in unit.PricePerson">
</select>
</span>
正如@cyan所建议的,我认为您还需要ng模型,而不是外部ng重复中的变量
希望这有帮助。您的问题有点不清楚,但您的ng选项配置似乎有问题 通常不建议将“选择为”与“跟踪方式”一起使用,因为它们不是为协同工作而设计的,可能会产生意外的结果。此外,您在ng选项中使用的是
unit.PricePerson
引用,而不是单个迭代对象personNum
您应该有如下内容:
<span ng-repeat="personNum in unit.PricePerson">
{{personNum}}
<select ng-model="personNum.selectedOption"
ng-options="o as o for o in unit.PricePerson">
</select>
</span>
{{personNum}}
但是,如果没有看到您的javascript对象
PricePerson
,我无法确定您的ng选项配置应该是什么。请看一看AngularJSAPI。在Arguments部分中,有建议的ng选项表达式模板 如果您希望所选选项为5、6、7等,您在其上设置的模型必须是您当前使用的值-personNum
。可能有助于查看您正在使用的数据(1个节点很好)否,首先必须有值5,然后是值6。etcI请理解,我的意思是,无论您在personNum
中设置了什么,都将设置您所选的选项,因为现在您已将其设置为选择列表中的模型。这将有助于查看您的数据。预选项目是否在您的数据集中?如果你能展示一下unit.PricePerson
的一个节点是什么样子的,那就更容易帮助了:)@cyan实际上是在正确的轨道上,我想。。。通过$index删除track,这个可能会起作用,尽管我认为ng模型可能需要在这里是一个数组,因为我们有一个select数组。
<span ng-repeat="personNum in unit.PricePerson">
{{personNum}}
<select ng-model="personNum.selectedOption"
ng-options="o as o for o in unit.PricePerson">
</select>
</span>