1

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.

3
  • How do you call greet? Commented Sep 15, 2013 at 13:55
  • I guess I don't, it was left over from an example. I just want it to run on page load, so is there a way of doing that? Commented Sep 15, 2013 at 13:58
  • Just add ng-init="greet()" in your <body> tag, Commented Sep 15, 2013 at 15:17

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

liked call from directive without pressing button +1
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.