Reputation: 13288
what is the use of the $window injection with Angular ?
for instance ,
var myController = function ($scope, $window) {
// window is still available here
};
myController.$inject = ['$scope', '$window'];
Upvotes: 0
Views: 90
Reputation: 1758
It is a known best practice to avoid using global variables when possible. Therefore, Angular provides you with a valid 'angular' way option to get the window object inside your code as a service. It is incredibly useful for tests to have that option.
Upvotes: 1
Reputation: 14037
Mainly for testability purpose as stated in Doc
While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In angular we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.
Upvotes: 1