Let’s say we have the following service:
myApp.factory('FooService', function () { ...
Then, from a controller, I would say:
myApp.controller('FooCtrl', ['$scope', 'FooService', function ($scope, FooService) { ...
The two-part question is:
- Global Accessibility: If I have 100 controllers and all need access to the service, I don’t want to explicitly inject it 100 times. How can I make the service globally available? Only thing I can think of at the moment is wrapping it from within the root scope, which defeats the purpose.
- Accessibility from view: How can I access the service from within the view? This post suggests wrapping the service from within the controller. If I am going to that length, seems I ought to just implement the functionality right on the root scope?
Found a reasonable solution. Inject it into the bootstrap method (run), and add it to the root scope. From there it will be available to all controllers and views.
Re-reading the post I mentioned above, it didn’t say “wrap” exactly… just “abstract”, so I presume the poster was referring to this same solution.
For thoroughness, the answer to (1) is then:
and the answer to (2) is simply:
Adding Christopher’s comment to make sure it’s seen: