Angular的Select选择框

数据源为数组时

<select ng-init="selectedName = names[0]" ng-model="selectedName" ng-options="x.title for x in names">
</select>

则其中每个option的text为{{x.title}}而选中时的ng-model为x对象
而如果用ng-repeat,每个option提供的ng-model即其value,只能为字符串

数据源是对象时

  • (x,y)对应着(key,value)
<select ng-model="selectedCar" ng-options="y.brand for (x, y) in cars">
</select>

$scope.cars = {
  car01 : {brand : "Ford", model : "Mustang", color : "red"},
  car02 : {brand : "Fiat", model : "500", color : "white"},
  car03 : {brand : "Volvo", model : "XC90", color : "black"}
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容