AngularJS ng-options具有对象属性值&标题

时间:2013-12-09 12:18:56

标签: javascript angularjs

我在我的范围内有这个数组

$scope.containers = [{id: 1, title: "Icebox"}, {id: 2, title: "Summer"}, 
{id: 3, title: "Milestone"}];

我可以创建类似ng-options的东西吗?

<option value="1">Icebox</option>
<option value="2">Summer</option>
<option value="3">Milestone</option>

3 个答案:

答案 0 :(得分:6)

您可以使用ng-options

<select ng-options="v.id as v.title for v in containers" ng-model="selectedId">        
</select>

Fiddle DEMO

答案 1 :(得分:3)

是的,如果您有兴趣以这种方式显示标题:

<select>
    <option
    ng-repeat="v in containers" 
    value="{{v.id}}" 
    title="{{v.title}}" 
    ng-selected="adresses.indexOf(v) == 0">{{v.title}}
    </option>
</select>

演示 Fiddle

但是,如果您只想显示value ng-options将是enogh

答案 2 :(得分:0)

我遇到了同样的问题,我被要求使用ng-options,这是我的解决方案......

<强> HTML:

<select ng-options="item.Id as item.Name for item in vm.listOfItems">
    <option disabled>Select a value</option>
</select>

<强>控制器:

vm.$onInit = function () {
    $timeout(function () {
         $("option").each(function (index, element) {
            var text = $(element).text();
            $(element).attr("title", text);
         });
    });
}