angularjs – ng-options和不显示angular.js的唯一过滤器

前端之家收集整理的这篇文章主要介绍了angularjs – ng-options和不显示angular.js的唯一过滤器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 JavaScript中,我有一个数组

$scope.quoteList =        
    [
        {
            select: false,laymansDescription: "Nathan",quoteNumber: "ING-70440-21",version: "02",quoteDate: "Feb 5,2013",expirationDate: "Aug 5,internalNotes: "This quote is using test data",},{
            select: false,laymansDescription: "Mitch",quoteNumber: "ING-70440-01",laymansDescription: "Stephen",}
    ];

而我正在尝试制作一个只显示唯一quoteNumbers的选择器,即. ING-70440-21和ING-70440-01.但是当我尝试在角度中使用’unique’选项时,没有任何东西出现.

<select class="form-control" ng-options="quote.quoteNumber for quote in quoteList | unique:'quoteNumber'" ng-model="quoteModel1" />

没有唯一标签,它工作正常.我究竟做错了什么?我对角度很新,所以它可能非常简单.

解决方法

AngularJS没有内置的唯一过滤器.你可以做这样的事情来制作你自己的:

app.filter('unique',function() {
    return function(input,key) {
        var unique = {};
        var uniqueList = [];
        for(var i = 0; i < input.length; i++){
            if(typeof unique[input[i][key]] == "undefined"){
                unique[input[i][key]] = "";
                uniqueList.push(input[i]);
            }
        }
        return uniqueList;
    };
});

猜你在找的Angularjs相关文章