在自定义过滤器中使用数组 [英] use array in custom filter
问题描述
我创建了一个具有以下格式的数组...
$scope.myArr = [arg1, arg2];
现在我想创建一个自定义过滤器,它将数组作为参数并与另一个数组进行比较,例如我想这样使用它...
这样每个 formblock
都会与数组进行比较,所以如果 formblock.date
有 arg1
或 arg2代码>然后这些将显示,否则隐藏所有其他内容.
这可能吗?
解决方案 您的 html 与自定义 Angular#Filter 应该是
您的 forms
作为第一个参数隐式传递,并在过滤器名称后使用 :
传递附加参数.
JS:
过滤器:
app.filter('dateFilter', function() {var boolFound = false;返回函数(arrForm,arrArg){arrForm.forEach(function(val, key) {var boolFound = false;arrArg.forEach(function(val1, key1) {如果(val.date === val1){boolFound = 真;}});if (boolFound === false) {arrForm.splice(key, 1);}});返回 arrForm;}})
这是更新的Fiddle
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?
解决方案 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
这篇关于在自定义过滤器中使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文