Reputation: 1687
Let us say I have a service named httpservice
, and let us say I have some m and n no of controllers and services, where m and n are positive integers and i want to use this service every where.
So my question is "Is there any way/mechanism in angularJs to inject this dependency httpservice only one time for all the different components of my application??
"
That means rather than writing code for injecting dependency for every controller and other services/directives/filters etc.
Upvotes: 0
Views: 70
Reputation: 1044
This sounds like an anti-pattern. It's unlikely a service is really needed in every controller and service of your application.
Following this architecture, you'd find yourself in a situation where the service is available, or 'globally injected', in parts of your application that don't need it. It's beneficial for both readability and performance to be explicit, even if this seems verbose.
This answer (as referenced in another answer) describes a way of almost doing this by attaching a reference variable to the $rootScope
, or relying on scope inheritance. However, this means it's only available in yours controllers and views, and not other services / factories – unless you pass in a $scope
reference.
Alternatively, and depending on the desired functionality of the service, an object literal attached to the global scope would be behave as if it were injected everywhere. This would be working against the intended design of Angular though.
Upvotes: 1