Question

It seems that the AngularJS $window.alert() and Javascript alert() are the same. Under what conditions should we use which one of them? Or are there no difference at all?

Was it helpful?

Solution

It is exactly the same thing - global methods in JavaScript (running in browser) are methods of the window. AngularJS' $window is alias for window object.

$window: A reference to the browser's window object.

Note that you may override $window for your tests, but in regular code both are the same.

OTHER TIPS

The difference is that in tests you can mock out the $window object so that instead of using the real alert it stubs to console.log or something else. If your code called alert directly then there is no way to override the real alert so in your tests you would see alerts popping up whenever your method used an alert.

Beyond just testing, in theory you could even use an alternative provider for $window which had an extra fancy alert implementation. Imagine if you wanted to use an alert built on bootstrap dialogs. Using the $window provider would permit you to change this in one place by injecting a new implementation and none of your other code would change.

You will not see a difference in your running code (both perform identically) but loosely coupling your code like this pays off later.

They are identical, because $window === window, unless you have overridden it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top