AngularJs - Use custom filter inside the controller

Scenario
I have an array of users containing information about them, I do ng-repeat in combination with a user directive that generates a custom HTML card, saving the volume of each card relative to an individual user, there is a value inside the user model that I need to filter using a custom filter before the template is compiled, because if I do this inside the template, the time it takes to filter will tell me that the tooltip will not be displayed until and the value will not be ready, and it seems that something is not working.

My code is still

 // userCard directive angular.module('userCard', []).directive('UserCard', function() { return { restrict: 'EA', templateUrl: 'userCard.tpl.html', scope: { user: '=' }, controller: ['$scope', 'fromNowFilter', function($scope, fromNowFilter) { angular.forEach($scope.user.reminders, function(reminder) { reminder.last_sent = reminder.last_sent === null ? 'No reminder has been sent!' : fromNowFilter(reminder.last_sent); }); }], link: function(scope, element) { // Add the base class to the user card element element.addClass('user-card'); } }; }); // fromNow custom filter angular.module('userCard').filter('fromNow', function() { return function(date) { return moment(date).fromNow(); }; }); // The error I keep getting Unknown provider: fromNowFilterProvider <- fromNowFilter 
+7
javascript angularjs angularjs-filter
source share
1 answer

Try entering filterprovider and run your filter.

 controller: ['$scope', '$filter', function($scope, $filter) { var fromNowFilter = $filter('fromNow'); angular.forEach($scope.user.reminders, function(reminder) { reminder.last_sent = reminder.last_sent === null ? 'No reminder has been sent!' : fromNowFilter(reminder.last_sent); }); }], 
+21
source share

All Articles