Introduction

The $window service in AngularJS refer to the browser window object. With reference to JavaScript, window is a global object that includes many methods like prompt, conform, alert, etc.

With window object in JavaScript, there is a testability problem due to it is defined as a global variable. In AngularJS, it can always refer a service called "$window" so that we may override (removed or mocked) for testing.

The $window service is just wrapped around the JavaScript window object. It is most recommended to use $window service with AngularJS application instead of using window object directly.

Example

Alert Example

  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <title>$window Service in AngularJS</title>
  5. <scriptsrc="angular.js">
  6. </script>
  7. </head>
  8. <bodyng-app="app">
  9. <h4>$window Service Example</h4>
  10. <divng-controller="HelloController">
  11. <buttonng-click="showAlert()">Alert Example</button>
  12. </div>
  13. <script>
  14. var app = angular.module("app", []);
  15. app.controller("HelloController", function($scope, $window) {
  16. $scope.showAlert = function() {
  17. $window.alert('Hi! Jignesh');
  18. };
  19. });
  20. </script>
  21. </body>
  22. </html>
Output

output

Prompt Example
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <title>$window Service in AngularJS</title>
  5. <scriptsrc="angular.js">
  6. </script>
  7. </head>
  8. <bodyng-app="app">
  9. <h4>$window Service Example</h4>
  10. <divng-controller="HelloController">
  11. <buttonng-click="showPrompt()">Prompt Example</button>
  12. </div>
  13. <script>
  14. var app = angular.module("app", []);
  15. app.controller("HelloController", function($scope, $window)
  16. {
  17. $scope.showPrompt = function()
  18. {
  19. var name = $window.prompt('Enter your fullName');
  20. $window.alert('Hi! ' + name);
  21. }
  22. });
  23. </script>
  24. </body>
  25. </html>
Output

output

Confirm Example
  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <title>$window Service in AngularJS</title>
  5. <scriptsrc="angular.js">
  6. </script>
  7. </head>
  8. <bodyng-app="app">
  9. <h4>$window Service Example</h4>
  10. <divng-controller="HelloController">
  11. <buttonng-click="showConfirm()">Confirm Example</button>
  12. {{result}}
  13. </div>
  14. <script>
  15. var app = angular.module("app", []);
  16. app.controller("HelloController", function($scope, $window) {
  17. $scope.showConfirm = function() {
  18. varconfirmExample = $window.confirm("Do you want to continue!");
  19. if (confirmExample == true) {
  20. $scope.result = "You pressed OK!";
  21. } else {
  22. $scope.result = "You pressed Cancel!”
  23. }
  24. }
  25. });
  26. </script>
  27. </body>
  28. </html>
Output

output

Browser Resize with AngularJS

The $window service can also be used to get the window resize event. To do this, we need to bind resize event with window object.

Controller Code
  1. var app = angular.module("app", []);
  2. app.controller("HelloController", function($scope, $window)
  3. {
  4. var window = angular.element($window);
  5. window.bind('resize', function()
  6. {
  7. console.log('resize');
  8. });
  9. }
Output

output

Summary

This article will help you to understand window service in AngularJS.