0

I'm starting with Angular JS and I'm stuck with a problem. I have the following function in the controller that applies a filter to a ng-repeat of a table.

    $scope.dateRangeFilter = function (property, startDate, endDate) {
if (startDate==null)
{
    startDate=moment('1900-01-01',"YYYY-MM-DD");
    var s=moment(startDate,"YYYY-MM-DD");

}
else
{
    var datestart = new Date(startDate);
    var s=moment(datestart.getFullYear() + '/' +  ("0" + (datestart.getMonth() + 1)).slice(-2) + '/' +  datestart.getDate()  ,"YYYY-MM-DD");
}
if (endDate==null)
{
    endDate=moment('2999-31-12',"YYYY-MM-DD");
    var e=moment(endDate,"YYYY-MM-DD");
}
else
{
    var datend = new Date(endDate);
    var e=moment(datend.getFullYear() + '/' + ("0" + (datend.getMonth() + 1)).slice(-2) + '/' +  datend.getDate()  ,"YYYY-MM-DD");
}
return function (item) {
    if (item[property] === null) return false;
    var itemDate = moment(item[property]);
    if (itemDate >= s && itemDate <= e) return true;
    return false;
}
}

Then I apply it in ng-repeat

<tr ng-repeat="user in plazas | filter: dateRangeFilter('date', fechaDesde, fechahasta)">

What I need is to be able to apply that same filter to the controller because I need to get the number of records with filters for other functionality.

$scope.getData = function () {
    return $filter('filter')($scope.plazas, ..............................)
}

Any ideas please? Thank you very much

beuqui
  • 1
  • Could be a duplicate question like https://stackoverflow.com/questions/14302267/how-to-use-a-filter-in-a-controller Basically you must inject $filter service to your controller and then call your filter like `$filter('nameOfYourFilter')(args_required_by_filter)` inside your controller – Fered Jul 02 '17 at 08:44
  • Replace `..............................` by `$scope.dateRangeFilter('date', $scope.fechaDesde, $scope.fechahasta)` – JB Nizet Jul 02 '17 at 09:13
  • JB Nizet Thank you so much! I tried so many things that I not looked at the simple things – beuqui Jul 02 '17 at 10:56

0 Answers0