Angular ng-options:通过id进行比较

时间:2015-01-19 09:53:55

标签: javascript angularjs

3 个答案:

答案 0 :(得分:1)

结帐http://jsfiddle.net/oj884ofm/

var app = angular.module('myApp', []);

app.controller('TestCtrl', function($scope) {

    $scope.selectedId = '1';

    $scope.items = {
        1: 'item1',
        2: 'item2'
    }   

});

答案 1 :(得分:0)

您应该尝试'key as value for (key, value) in items':

<div ng-controller='TestCtrl'>
    <select 
        ng-init="selectedId='1'"
        ng-model='selectedId'
        ng-options='key as value for (key, value) in items'>
    </select>
</div>

答案 2 :(得分:0)

您绝对应该考虑使用数组而不是对象。你仍然可以用数组说'items [2]'。 Here是小提琴和代码。

使用Javascript:

var app = angular.module('myApp', []);
app.controller('TestCtrl', function ($scope) {
   $scope.items = ['item1', 'item2'];
   $scope.selectedId = $scope.items[0];
});

HTML:

<div ng-controller='TestCtrl'>
 <select ng-model='selectedId' ng-options='item for item in items'></select>
</div>