Angularjs实现下拉框自动匹配键值对对象技巧

前端之家收集整理的这篇文章主要介绍了Angularjs实现下拉框自动匹配键值对对象技巧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用到的对象:

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

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

要求:根据 选择的 studentAddressFields的项自动匹配到student 名称

方法1:最直接的方法

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

方法2:使用filter

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

Filter JS代码:
myApp.filter('getOrdered',function() {
    return function(input) {
        var ordered = {};
        for (var key in input){            
            ordered[input[key]["code"]] = input[key];
        }           
        return ordered;
    };
});
原文链接:https://www.f2er.com/angularjs/148242.html

猜你在找的Angularjs相关文章