Let’s take the example from AngularJS tutorial
function PhoneListCtrl($scope, $http) {
$http.get('phones/phones.json').success(function(data) {
$scope.phones = data;
});
$scope.orderProp = 'age';
}
//PhoneListCtrl.$inject = ['$scope', '$http'];
Now, lets say I don’t want to hard code the url 'phones/phones.json' and would prefer the page which hosts this controller to inject it, what should be the right way of doing the same in Angular JS?
There are a lot of ways to do this… the simplest way would just be to use
$window, so you’d inject the $window service, and that’s basically just the global $window that’s been injected. Then you can register those paths aswindow.path = 'whatever.json';and you’ll be fine:A more advanced way would be to create a module with a service that you inject into your app, in this case each page would have it’s own module:
You can, of course, do anything in between the two. I would suggest taking the path that is the most maintainable while still easy to test.