angular.js - filter data parameter passing problem in angular
漂亮男人
漂亮男人 2017-05-15 17:08:49
0
2
929

Controller:$scope.dataList=[

{"dataClass":"0","dataName":"A1"},
{"dataClass":"0","dataName":"A2"},
{"dataClass":"1","dataName":"B1"},
{"dataClass":"1","dataName":"B2"},
{"dataClass":"2","dataName":"C1"},
{"dataClass":"2","dataName":"C2"}

]
HTML template:
<li ng-repeat="item in dataList |filter :{'dataClass':'0'}">{{item.dataName}}</li>
The page display will loop to filter out A1 and A2 whose dataClass is '0' in the dataList array; current requirements The value filtered by dataClass is a variable set to variability, and ng-repeat="item in dataList |filter :{'dataClass':varity}", and it does not take effect. How can I achieve this requirement

漂亮男人
漂亮男人

reply all(2)
为情所困

I wrote a filter for you and removed the specified dataClass:

angular.module('common', []).filter('myFilter', function () {
        return function (collection, keyname,value) {
            var output = [];
            angular.forEach(collection, function (item) {
                //过滤数组中值与指定值相同的元素
                if(item[keyname]!=value){
                    output.push(item);
                }
            });
            return output;
        }
    });

<p ng-app="myApp" ng-controller="myCtrl">
    <p ng-repeat="x in items | myFilter: 'dataClass': dcValue ">
        {{x.dataClass}}---{{x.dataName}}
    </p>
</p>

The filter receives 3 parameters, the original array, the key to be filtered, and the specified key value.

phpcn_u1582
repeat="item in dataList |filter :{'dataClass':{{varity}} }"

Just add curly brackets

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template