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.
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.
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.
Try:
app.filter('sageApp', ['segmentio', function(segmentio) { return function(entry, category) { segmentio.track(entry, category); } }]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With