I'm writing a custom filter which depends on a constant object named config
. This is what I've got so far:
angular.module('myproject.filters', []).filter('userLink', function () {
return function (user) {
return '';
};
});
I need to inject the 'config
' module into my filter so I can pull a static value out of it for building the link to a given user object.
How do I inject dependencies into filter functions?
You can inject a service or a constant as a dependency like this
angular.module('myproject.filters', [])
.filter('userLink', ['config', function (config) {
return function (user) {
if (user == null)
return "#";
return config.appRoot + '/user/' + user.id + "/";
};
}]);
If this depends on a config
module like you mention, be sure to include that in your module statement.
You can inject dependencies into filters the same way you inject into controllers, services and factories.
angular.module('myproject.filters', ['config'])
.filter('userLink', ['$compile', '$timeout', 'configService', function ($compile, $timeout, configService) {
return function (user) {
return '';
};
}]);
As eddiec pointed out if config
is a module you must include it as a dependency for your myproject.filters
. Then to include a service within the config
module in the filter you inject it within the filter method.