简体   繁体   English

无法使用多个参数在ng-repeat中使用过滤器

[英]Can't get filter to work in ng-repeat with multiple parameters

I've used filters in my app before, but this is the first time I've needed to send extra parameters to the filter. 我以前在我的应用程序中使用了过滤器,但这是我第一次需要向过滤器发送额外的参数。 There seems to be an implicit parameter that gets passed to the filter when used like this: 似乎有一个隐式参数在使用时传递给过滤器,如下所示:

ng-options="t as t.TeamName for t in (teamList | filter: filterTeams) track by t.TeamId">

In my controller I have: 在我的控制器中我有:

        $scope.filterTeams = function (team) {
            if ($scope.EU.selectedUsersDepartment && !isEmpty($scope.selectedUsersDepartment)) {
                return (team.Department.DepartmentId === $scope.EU.selectedUsersDepartment.DepartmentId);
            } else {
                return true;
            }
        };

team seems to be passed to filterTeams implicitly. team似乎是隐式传递给filterTeams

When I try to extend this idea with multiple parameters it fails: 当我尝试用多个参数扩展这个想法时,它失败了:

ng-repeat="listEntry in (list | filter: filterByDay(listEntry, 1))"

Controller code: 控制器代码:

        $scope.filterByDay = function (listEntry, dayOffset) {
            return isDateEqual(listEntry.myDate, addDays($scope.NIC.selectedWeek, dayOffset)) ||
                   isDateEqual(listEntry.myOtherDate, addDays($scope.NIC.selectedWeek, dayOffset));
        };

listEntry is always undefined, but dayOffset comes through fine. listEntry总是未定义的,但是dayOffset很好。 It seems explicit passing of the array element fails, but having any kind of parameter in the template also destroys the implicit passing. 似乎数组元素的显式传递失败,但模板中有任何类型的参数也会破坏隐式传递。

I've tried alternative syntax as suggested in this answer: 我已经尝试了这个答案中建议的替代语法:

How do I call an Angular.js filter with multiple arguments? 如何使用多个参数调用Angular.js过滤器?

ng-repeat="listEntry in (list | filterByDay: listEntry:1)"

However this also fails. 然而,这也失败了。

This next answer discusses an option of using a dedicated angular filter in option 2: 下一个答案讨论了在选项2中使用专用角度滤波器的选项:

How to use parameters within the filter in AngularJS? 如何在AngularJS中使用过滤器中的参数?

But I'm not clear on the difference between a function attached to $scope being used as a filter and adding a filter to my root app module, nor does the example demonstrate how to pass multiple parameters. 但是我不清楚附加到$scope的函数被用作过滤器和向我的根应用程序模块添加过滤器之间的区别,该示例也没有演示如何传递多个参数。

I also tried following this answer which suggests returning a function in your filter: 我也尝试按照这个答案建议在过滤器中返回一个函数:

Passing arguments to angularjs filters 将参数传递给angularjs过滤器

I modified my ng-repeat to this: 我修改了我的ng-repeat到这个:

ng-repeat="listEntry in (list | filter: filterByDay(1))"

And my filterByDay function to this: 我的filterByDay函数:

        $scope.filterByDay = function (dayOffset) {
            return function (listEntry) {
                console.log('listEntry: ');
                console.log(listEntry);
                console.log('dayOffset: ');
                console.log(dayOffset);
                return isDateEqual(listEntry.myDate, addDays($scope.NIC.selectedWeek, dayOffset)) ||
                    isDateEqual(listEntry.myOtherDate, addDays($scope.NIC.selectedWeek, dayOffset));
            }
        };

This seems to prevent the function returned by filterByDay from working, with none of the console.log statements executing. 这似乎阻止filterByDay返回的函数工作,没有执行任何console.log语句。

I was unable to get filters working with multiple parameters but I found an alternative that works just as well. 我无法让过滤器使用多个参数,但我找到了一个同样有效的替代方案。

Replace: 更换:

ng-repeat="listEntry in (list | filter: filterByDay(1))"

with

ng-repeat="listEntry in getFilteredList(list,1)"

getFilteredList is a function attached to $scope in my controller that iterates over list and returns an array of just the elements I want. getFilteredList是一个附加到我的控制器中的$scope的函数,它遍历list并返回一个只包含我想要的元素的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM