angularjs select 赋值 ng-options配置方法

时间:2021-05-28

数组方式

数据是数组

$scope.years = [2014, 2015, 2016];

页面元素

<select ng-model="item" ng-options="item as y for y in years"> </select>

设置默认值

如果需要设置默认的选项,可以先设置一个参数:

$scope.item = 2016;$scope.years = [2014, 2015, 2016];

对象数组方式

数据是对象数组

$scope.sites = [ {site : "baidu", url : "https://"}];

Key-Vules对象数组方式

数据是对象数组

$scope.cars = {car1 : {brand : "BYD", model : "Y50", color : "red"},car2 : {brand : "CC", model : "HF", color : "white"},car3 : {brand : "JL", model : "JL10D", color : "black"}};

页面元素

<select ng-model="myCar" ng-options="y.brand for (x, y) in cars"></select>

设置默认值

如果需要设置默认的选项,可以先设置一个参数:

$scope.site = "BYD";$scope.cars = {car1 : {brand : "BYD", model : "Y50", color : "red"},car2 : {brand : "CC", model : "HF", color : "white"},car3 : {brand : "JL", model : "JL10D", color : "black"}};

angularjs ng-options官方API

数组类型:

label for value in arrayselect as label for value in arraylabel group by group for value in arrayselect as label group by group for value in array track by trackexpr

对象类型:

label for (key , value) in objectselect as label for (key , value) in objectlabel group by group for (key, value) in objectselect as label group by group for (key, value) in ob

以上这篇angularjs select 赋值 ng-options配置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章