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.
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. 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?
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:
How to use parameters within the filter in 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.
I also tried following this answer which suggests returning a function in your filter:
Passing arguments to angularjs filters
I modified my ng-repeat to this:
ng-repeat="listEntry in (list | filter: filterByDay(1))"
And my filterByDay
function to this:
$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.