Jimmy
Jimmy

Reputation: 12487

AngularJS - Window alert onload

I am just using the fantastic library that is angularJS. I am trying to get a simple window alert to come up on page load. This is the code I am using:

$scope.greet = function() {
    ($window.mockWindow || $window).alert('Hello World');
  }
}

I am new to javascript generally and it seems I have made a mistake with this because it doesn't work:

http://jsfiddle.net/AQ533/5/

Can anyone show me what it should be please? Thank you.

Upvotes: 1

Views: 5314

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

I don't sure what you want to see with your example but this is working code:

<html ng-app>
 <div ng-controller="TodoCtrl">
    <span id="logo">Just a</span><span id="small" >PREVIEW</span>

   <button class="btn-large" ng-click="greet()">Press</button>
</div>

Controller

function TodoCtrl($scope, $window) {
  $scope.greet = function() {
  ($window.mockWindow || $window).alert('Hello');
  }
}

DEMO in Fiddle

Hope it will help you

Upvotes: 1

Ivan Chernykh
Ivan Chernykh

Reputation: 42166

Don't forget to inject $window service:

app.controller('MainCtrl', function($scope , $window) {
  $scope.greet = function() {
    ($window.mockWindow || $window).alert('Hello');
  }
});

Working example: http://plnkr.co/edit/uO9l7n?p=preview

Upvotes: 1

Related Questions