Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a service as dependency for a custom filter in angular.js?

Tags:

angularjs

I am trying to create a custom filter to track events. So the filter can call methods on the segmentio service.

angular.module('sageApp')   .filter('trackEvent', function(segmentio) {     return function(entry, category) {      segmentio.track(entry, category);     } }); 

But the segmentio service is not available. Any ideas on how to dependency inject a service to a filter woule be much appreciated.

like image 440
Satheesh Kumar Avatar asked Jul 16 '13 22:07

Satheesh Kumar


People also ask

Can be injected as a dependency in AngularJS?

Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider.

Which API is used to create a custom filter in AngularJS?

In exactly the same way, AngularJS has given us the angular. filter API to create a custom filter in AngularJS. The returned function will have custom filter code and it will return the output.


1 Answers

Try:

app.filter('sageApp', ['segmentio', function(segmentio) {     return function(entry, category) {         segmentio.track(entry, category);     } }]); 
like image 157
zs2020 Avatar answered Sep 21 '22 13:09

zs2020