vdegenne
vdegenne

Reputation: 13288

injection of $window in angular, what for?

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

Answers (2)

SirDemon
SirDemon

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

squiroid
squiroid

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

Related Questions