top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between $window and window in AngularJS?

0 votes
621 views
What is difference between $window and window in AngularJS?
posted Nov 18, 2017 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

$window is an Angular service which reference to the browser's window object. The window object is
globally available in JavaScript; it causes testability problems, because it is a global variable. Angular refer to it
through the $window service, so that it can be overridden, removed or mocked for testing.

<script>
 var app = angular.module('windowExample', []);
 app.controller('ExampleController',function ($scope, $window) {
 $scope.greeting = 'Hello, World!';
 $scope.doGreeting = function (greeting) {
 $window.alert(greeting);
 };
 });
</script>
<div ng-app="windowExample" ng-controller="ExampleController">
 <input type="text" ng-model="greeting" />
 <button ng-click="doGreeting(greeting)">ALERT</button>
</div>
answer Nov 18, 2017 by Shivaranjini
...