Angularjs Examples

AJS Examples


AngularJS $window Service

The AngularJS $window is similar to JavaScript window object. In angular it is refer to it through the $window service. In the JavaScript, window is a global object which includes many built-in methods like alert(), prompt() etc. The $window service in AngularJS is a wrapper around window object, so that it will be easy to override, remove or mocked for testing.

Syntax

$window.alert(message);
var name = $window.prompt('Enter Your Name');
$window.alert('Hello ' + name); 

AngularJS $window Service


<!DOCTYPE html>
<html>
<head> <!-- www.techstrikers.com -->
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
    <meta charset="utf-8">
    <title>AngularJS $window Example</title>
  <script>
var app = angular.module('app', []);
    app.controller("windowController", function ($scope, $window) {
      $scope.AngulerAlert = function (message) {
        $window.alert(message);
      }
      $scope.AngulerPrompt = function () {
        var value = $window.prompt('Please enter value');
        $window.alert('Your Value : ' + value);
      }
    });
</script>
</head>
<body style="background-color:#DDE4E9;">
  <fieldset style="background-color:#DDE4E9;">            
    <legend>AngulerJS $window Service Example</legend> 
  <div ng-app="app">      
    <div ng-controller="windowController">      
        <button ng-click="AngulerAlert('Hello!!')">Alert Window</button>
    <button ng-click="AngulerPrompt()">Prompt Window</button>      
    </div>      
</div>
 </fieldset> 
</body>
</html>