admin管理员组文章数量:1208155
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
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
Share
Improve this question
edited Aug 29, 2014 at 2:12
PSL
124k21 gold badges256 silver badges243 bronze badges
asked Aug 29, 2014 at 1:09
CupOfJoeCupOfJoe
5191 gold badge5 silver badges14 bronze badges
1 Answer
Reset to default 21Try 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);
});
}],
本文标签: javascriptAngularJsUse custom filter inside directive controllerStack Overflow
版权声明:本文标题:javascript - AngularJs - Use custom filter inside directive controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738689223a2107005.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论