最近在angular中使用select指令時,出現(xiàn)了很多問題,搞得很郁悶。查看了很多資料后,發(fā)現(xiàn)select指令并不簡單,決定總結(jié)一下。
select用法:
<selectng-model=""[name=""][required=""][ng-required=""][ng-options=""]></select>
屬性說明:
發(fā)現(xiàn)并沒有ng-change屬性
ng-required:當(dāng)屬性值為true時,對select添加required驗(yàn)證,為false時不驗(yàn)證。
ng-options:指定數(shù)據(jù)源,生成option選項(xiàng)。
數(shù)據(jù)源為數(shù)組時,用法:
label for value in array
select as label for value in array
label group by group for value in array
select as label group by groupexpr for value in array track by trackexpr
數(shù)據(jù)源為對象時,用法:
label for (key,value)in object
select as label for(key,value)in object
label group by group for(key,value)in object
select as label group by groupexpr for (key,value)in object track by trackexpr
具體說明:
array/object:數(shù)組/對象
label:option選項(xiàng)顯示的名稱。
select:是選中某一項(xiàng)后,綁定到ngModel的值。
value : 數(shù)組中的值。
(key,value):對象的key,value。
group by groupexpr:用于選項(xiàng)分組,
ng-options與ng-repeat自動生成option選項(xiàng)的區(qū)別:
ng-options生成的option選項(xiàng)選中后,綁定到ngModel的值可以是對象。ng-repeat綁定到ngModel的值只能是字符串。
注意:select初始化時需要為ngModel指定值,否則會出現(xiàn)空白選項(xiàng)。
以上所述是小編給大家介紹的Angular 中 select指令用法詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對錯新站長站網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選