Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am playing around with different ways of loading an alert on load with angularJS. I saw this method documented:

http://jsfiddle.net/AQ533/9/

JS

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

HTML

<div data-ng-controller="myCtrl" data-ng-init="init()">
<span id="logo">Just a</span><span id="small" >PREVIEW</span>    
</div>

I get the following error:

Uncaught ReferenceError: $scope is not defined

Can anyone tell me what I am doing wrong with this implementation please?

share|improve this question
2  
Wait, if I'm not mistaken $scope only exists inside a controller, whereas your code isn't. In that case it's not so strange $scope isn't defined. – 11684 Sep 15 '13 at 16:48
    
i've posted this link in your previous question' page , have a look: plnkr.co/edit/uO9l7n?p=preview – Cherniv Sep 15 '13 at 16:50
    
@chemiv Yours seems like another implementation, with a lot more code though – Jimmy Sep 15 '13 at 16:55
up vote 5 down vote accepted

You must define myCtrl in order to have $scope available:

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

Here is an updated fiddle.

Edit: I had to wrap your fiddle with a div that included ng-app. ng-app tells angular where the global scope of the application is. This allows it to compile everything inside and see if it can find any controllers etc.

Also, $scope must be passed in to the implementation of controller myCtrl with any dependencies afterward (in this case $window service).

share|improve this answer
    
Thank you for the reply, but your fiddle still doesn't show the alert on page load, is something else missing? – Jimmy Sep 15 '13 at 16:56
    
@Jimmy yes, sorry about that, I've updated the fiddle. I had to add ng-app and use the built-in way of initializing angular. – Davin Tryon Sep 15 '13 at 17:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.