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

原创
2017/01/21 10:08
阅读数 1.2K
AI总结

使用到的对象:

$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;
    };
});

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
AI总结
返回顶部
顶部