我正在尝试使用包含字符串的数组过滤ng-repeat
,如何使用多个过滤器或如何使用下面的数组直接过滤?
过滤数组:
["Thu","Fri","Mon","Tue","Wed","Thu","Fri"]
ng-repeat使用的数组:
["Fri, November 18th 2016","Sat, November 19th 2016","Sun, November 20th 2016","Mon, November 21st 2016","Tue, November 22nd 2016","Wed, November 23rd 2016","Thu, November 24th 2016"]
JSP:
<div class="form-group" ng-repeat="vdates in view | filter : selecteddays ">
答案 0 :(得分:0)
为什么不使用自己的自定义过滤功能来检查列表中至少有一个预期天数是否与您的输入值匹配?
我不确定我们是否可以使用内联过滤进行这种处理(使用条件指定数组和过滤器:至少在流与数组的任何元素之间匹配)。
这是plunkr。
HTML:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.5.x" src="https://code.angularjs.org/1.5.8/angular.js" data-semver="1.5.8"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div class="form-group" ng-repeat="vdates in view | filter : myFilter">
{{vdates}}
</div>
</body>
</html>
JS:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.view = ["Fri, November 18th 2016","Sat, November 19th 2016",
"Sun, November 20th 2016","Mon, November 21st 2016",
"Tue, November 22nd 2016","Wed, November 23rd 2016",
"Thu, November 24th 2016" ];
$scope.selecteddays=["Thu","Fri","Mon","Tue","Wed","Thu","Fri"];
$scope.myFilter = function(value) {
if (!value){return false}
for (var i = 0; i < $scope.selecteddays.length; i++) {
currentAcceptedDay = $scope.selecteddays[i];
if (value.indexOf(currentAcceptedDay) != -1) {
return true;
}
}
return false;
}
});