一尘不染

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

angularjs

在我的JavaScript中,我有一个数组

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

而且我正在尝试使选择器仅显示唯一的quoteNumbers即。ING-70440-21和ING-70440-01。但是,当我尝试在angular中使用’unique’选项时,没有任何显示。

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

没有唯一标签,它可以正常工作。我究竟做错了什么?我对angular非常陌生,所以它可能真的很简单。


阅读 197

收藏
2020-07-04

共1个答案

一尘不染

AngularJS没有内置unique过滤器。您可以执行以下操作来制作自己的一个:

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;
    };
});
2020-07-04