如何在角度中使用ng-options,其中Label和value相同

时间:2015-06-23 09:36:24

标签: angularjs

我正在尝试使用ng-options创建下拉列表,但在编辑页面上它无效。

js file:

$scope.items = [
    {name:"Angular"},
    {name:"java script"},
    {name:"Css3"},
    {name:"Html5"},
];

$scope.name= "Css3";

html文件:

<div class="form-group">Name</label>
        <select class="form-control" ng-model="name" ng-options="a.name for a in items track by a.name" placeholder="Select Item"></select>
</div>

工作正常。 但我得到了这样的选择值:

name:{
     name:"selectedValue"
}

但我想这样:

name:"selectedValue"
in $scope.selected after selected value from dropdown.

我提到了blog

在通过inspect元素比较@ Satpal的演示和我的输出之后得到这个:

  

我的输出

<select class="form-control ng-pristine ng-valid" ng-model="name" ng-options="a.name as a.name for a in items" placeholder="Select Item">
    <option value="0" selected="selected" label="Angular">Angular</option>
    <option value="1" label="java script">java script</option>
    <option value="2" selected="selected" label="Css3">Css3</option>
    <option value="3">Html5</option>
</select>
  

Satpal的输出:

<select class="form-control ng-pristine ng-valid" ng-model="name" ng-options="a.name as a.name for a in items" placeholder="Select Item">
    <option value="0">Angular</option>
    <option value="1">java script</option>
    <option value="2" selected="selected">Css3</option>
    <option value="3">Html5</option>
</select>

1 个答案:

答案 0 :(得分:3)

使用以下ngOption

表达式
  

选择数组中值的标签

代码

a.name as a.name for a in items

DEMO