1

我只是询问了从键/值映射而不是数组生成选择:AngularJS select box generated from object

现在一切正常:http: //jsfiddle.net/UPWKe/1/

<select ng-model="current.addressCode" ng-options="value.code as value.name for (key, value) in student.address | orderBy: 'code'"</select>

...和js ...

$scope.student = {
    address: {
        select: {
            code: "0",
            name: "Select proof of address"
        },
        letter: {
            code: "1",
            name: "Letter"
        },
        photograph: {
            code: "3",
            name: "Photograph"
        }
    },

但唯一缺少的是如何订购所选项目。

如何在从 angularjs 中的键/值映射生成的选择框中订购选择项目?

4

1 回答 1

5

解决方案 1:您可以使用另一个数组来存储字段的顺序。ng-repeat为此,您需要使用ng-options

$scope.studentAddressFields = [
    "select",
    "letter",
    "photograph"
]

HTML:

<select ng-model="current.addressCode">
    <option ng-repeat="field in studentAddressFields" 
    value="student.address[field]['code']">
        {{student.address[field]['name']}}
    </option>
</select>

解决方案 2:使用过滤器:

HTML:

<select ng-model="current.addressCode" ng-options="code as details.name 
for (code, details) in student.address | getOrdered">
</select>

筛选:

myApp.filter('getOrdered', function() {
    return function(input) {
        var ordered = {};
        for (var key in input){            
            ordered[input[key]["code"]] = input[key];
        }           
        return ordered;
    };
});
于 2013-09-06T09:33:14.393 回答