$Filter are very useful when used with the view part of the application. Most of the time we only need to filter the UI according to our requirement. But There are cases where we want to make use of Angular’s all powerful $Filter in the controller or in Service/Factory.

Here in this article i will show how we can easily use $filter/ custom filter in Angular Controllers:

var app = angular.module('sampleApp', []);
// we need to provide $filter as dependency for the controller
app.controller('SampleCtrl', ['$scope','$filter', function($scope, $filter){
// method to round of the value and feed to Points variable     
     $scope.roundOff = function(inputValue, placeUpto){
// decimalUpto is the name of the custom filter we created in another post
           $scope.Points = $filter('decimalUpto')(inputValue, placeUpto);
     }
// calling the roundoff method with inputValue = 12.029019 and placeUpto = 2;
     $scope.roundOff(12.029019, 2);
}]);

We are making the use of the custom filter we created in another post linked here: Rounding Off in Angularjs.

Hope the concept is clear and let me know how you feel about the article.
#knowledge_is_for_sharing




Leave a Reply

Your email address will not be published. Required fields are marked *