2

how to create Custom filter angularjs javascript controller side? i would like to search in array called segments by SegmentId, to create filter that do foreach on segments array search by SegmentId -

    //Controller

    $scope.GetSegmentDetails = function (id) {
        $scope.SegmentName = $filter('SegmentById')($scope.segments,id);
    }


    //Filter
    app.filter('SegmentById', function () {
        return function (input, searchPerson) {
        if (!searchPerson)
            return input;

        var results = [];
        angular.forEach(input, function (person) {
        }
    });

    return results;
}

});

hod caspi
  • 826
  • 2
  • 10
  • 17
  • please post an example model (i.e. `$scope.segments`) – boindiil Nov 04 '14 at 15:21
  • I don't understand you want to create a filter to put logic in the controller? then you don't need a filter you can do it directly in the controller, filters are used to filter displayed data binded in HTML – Charlie Nov 04 '14 at 15:22
  • @boindiil [{"SegmentId":"1","Description":"hod Registrations"}, {"SegmentId":"2","Description":"hod Inactive"}, {"SegmentId":"3","Description":"hod testUpd"}, {"SegmentId":"8","Description":"hod test"}, {"SegmentId":"1111","Description":"hod Release"}, {"SegmentId":"12","Description":"hod Requests"}, {"SegmentId":"13","Description":"hod Welcome Back"}] – hod caspi Nov 04 '14 at 15:31
  • @Charlie yes i know i can do it directly as private function, there is also a need to do it on the page it self, the same functionality... – hod caspi Nov 04 '14 at 15:32
  • oh ok well have you tried to import the filter object of your angular module in your controller? because I am not sure components in a module know about each others implicitly not sure how to do it but you should find smth with google – Charlie Nov 04 '14 at 15:43

2 Answers2

4

You dont have to write your one filter to filter by SegmentId. Here you have an example

function MyCtrl($scope, $filter) {
  $scope.data = [{"SegmentId":"1","Description":"hod Registrations"}, {"SegmentId":"2","Description":"hod Inactive"}, {"SegmentId":"3","Description":"hod testUpd"}, {"SegmentId":"8","Description":"hod test"}, {"SegmentId":"1111","Description":"hod Release"}, {"SegmentId":"12","Description":"hod Requests"}, {"SegmentId":"13","Description":"hod Welcome Back"}]
  
  $scope.filterData = function(segmentId) {
    return $filter('filter')($scope.data, { SegmentId: segmentId });
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="MyCtrl">
  Full:
  <ul>
    <li ng-repeat="Segment in data">
      {{Segment.SegmentId}}
    </li>
  </ul>
  Filtered in View:
  <ul>
    <li ng-repeat="Segment in data | filter:{SegmentId:1111}">
      {{Segment.SegmentId}}
    </li>
  </ul>
  Filtered in Controller:
  <ul>
    <li ng-repeat="Segment in filterData(1111)">
      {{Segment.SegmentId}}
    </li>
  </ul>
</div>
boindiil
  • 5,805
  • 1
  • 28
  • 31
1

Only need to add in your controller the filter dependency and then call it.

app.controller("YourController", ['$scope', '$filter', function ($scope, $filter){

$filter('filterName')($args));

}]);