Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  1] [ 4]  / answers: 1 / hits: 21860  / 10 Years ago, fri, august 29, 2014, 12:00:00

Scenario

I have an array of users containing information about them, I do an ng-repeat combined with a custom directive that generates an HTML user card, keeping the scope of each card relative to the individual user, inside the user model there is a value that I need to filter with a custom filter before the template gets compiled, because if I do it inside the template the time it takes to be filtered makes the tooltip not to show until the value is ready and that looks as if something is not working.



My code so far



// 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

More From » angularjs

 Answers
29

Try inject 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);
});
}],

[#69621] Tuesday, August 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrell

Total Points: 109
Total Questions: 113
Total Answers: 113

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;