use array in custom filter

痴心易碎 提交于 2020-01-06 18:10:37

问题


I created an array that has the following format...

$scope.myArr = [arg1, arg2];

Now I want to create a custom filter that will take the array as a parameter and compare to another array, for example I want to use it as so...

<div class="form-container" ng-repeat="formblock in forms | filter:dateFilter(myArr)">

This way every formblock will be compared to the array, so if formblock.date has either arg1 or arg2 then these will show, otherwise hide everything else.

Is this possible?


回答1:


Your html with custom Angular#Filter should be

<div class="form-container" ng-repeat="formblock in forms | dateFilter:myArr">

Your forms is passed as firsr parameter implicitely and passed the additional parameter with : after filter name.

JS :

Filter :

app.filter('dateFilter', function() {

    var boolFound = false;
    return function(arrForm, arrArg) {

        arrForm.forEach(function(val, key) {
            var boolFound = false;
            arrArg.forEach(function(val1, key1) {

                if (val.date === val1) {
                    boolFound = true;
                }
            });
            if (boolFound === false) {
                arrForm.splice(key, 1);
            }
        });
        return arrForm;
    }
})

Here is the updated Fiddle



来源:https://stackoverflow.com/questions/39279117/use-array-in-custom-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!