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
为情所困2017-05-15 17:10:49
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_u15822017-05-15 17:10:49
repeat="item in dataList |filter :{'dataClass':{{varity}} }"
Just add curly brackets