Angular.js在ng-repeat中设置<select>选项

时间:2016-02-18 21:07:11

标签: javascript angularjs

我试图将选项设置为ng-repeat中的值? 代码看起来应该&#39;应该&#39;工作,但它没有设置选择选项。我错过了什么? &lt; tr ng-repeat =&#34;拆分为splitDetails.Splits&#34;&gt;     &LT; TD&GT;         &lt; select style =&#34; width:150px;&#34; NG-模型=&#34; splitAdjusterTo&#34;&GT;             &lt; option value =&#34;&#34;&gt; - 选择一个 - &lt; / option&gt;             &lt; option ng-repeat =&#34; splitTo in splitDetails.PossibleAdjusters&#34;                     值=&#34; {{splitTo.AdjusterId}}&#34;                     ng-selected =&#34; {{splitTo.AdjusterId === split.ToAdjusterId}}&#34;&gt; {{splitTo.Name}}             &LT; /选项&GT;         &LT; /选择&GT;     &LT; / TD&GT; &LT; / TR&GT; 如果你愿意,拿这个json: {   &#34;水果&#34;:[     {&#34; id&#34;:1,&#34; name&#34; :&#34;橘子&#34;},     {&#34; id&#34;:2,&#34; name&#34; :&#34;香蕉&#34;},     {&#34; id&#34;:3,&#34; name&#34; :&#34;苹果&#34;}   ]   &#34;命令&#34;:[     {&#34; id&#34;:1,&#34; name&#34; :&#34; mark&#34;,&#34; fruitId&#34; :2},     {&#34; id&#34;:2,&#34; name&#34; :&#34; sean&#34;,&#34; fruitId&#34; :1},     {&#34; id&#34;:3,&#34; name&#34; :&#34; dave&#34;,&#34; fruitId&#34; :3},     {&#34; id&#34;:4,&#34; name&#34; :&#34; john&#34;,&#34; fruitId&#34; :2}   ] } 循环执行订单并使用fruitId填充选择...有意义吗?

1 个答案:

答案 0 :(得分:0)

使用ng-options,你应该能够做到这样的事情:

<select ng-model='split.ToAdjusterId' ng-options='splitTo.Name for splitTo in splitDetails.PossibleAdjusters track by splitTo.ToAdjusterId' ></select>

但是,使用ng-options不允许您插入“ - 选择一个 - ”的默认选项。执行此操作的最佳方法是确保splitDetails数组中的第一项等于以下内容:

{Name:"--Select One--", ToAdjusterId: ""}

并确保在控制器中split.ToAdjusterId默认为“”。