37

I'd like to do some stuff when my app loads to set up the default state. So I'm trying to use the run method on the Module object. When I try to access the $scope variable though I get an "Uncaught ReferenceError: $scope is not defined" message in my console.

See the following example http://jsfiddle.net/F2Z2X/1/

app = angular.module('myapp', []);

app.controller('mycontroller', function($scope){
    $scope.data = { myvariable: 'Hello' };
});

app.run(
    alert($scope.data.myvariable))
);

Am I going about this all wrong?

For example, I want to run the watchAction function once at the beginning, to hide UI elements that aren't called for yet, but the watchAction function doesn't have the $scope object because it's not being called by the watch method so I have to pass it to it, but alas it's not available.

1
  • .run gets run once at the very beginning of initialization. I don't think it makes a lot of sense to have a $scope at that point. You can pass in $rootScope tho. Commented Jun 28, 2013 at 19:06

2 Answers 2

78
app.run(function ($rootScope) {
    $rootScope.someData = {message: "hello"};
});

You can only get $rootScope injected to services and run function, because each child scope is inherited from its parent scope and the top level scope is rootScope. Since it would be ambigous to inject any scope. Only root scope is provided.

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

1 Comment

1 question: If I add any event handlers in the run block like $rootScope.on('myevent',function(){}).... how should I call $destroy? On $rootScope itself? Because if I don't do, I get lint error.... and can't use $scope in run block.
3
var app = angular.module('myApp', []);
app.run(function ($rootScope) {
    // use .run to access $rootScope
    $rootScope.rootProperty = 'root scope';
});

app.controller("ParentCtrl", ParentCtrlFunction);
app.controller("ChildCtrl", ChildCtrlFunction);
function ParentCtrlFunction($scope) {
    // use .controller to access properties inside ng-controller
    //in the DOM omit $scope, it is inferred based on the current controller
    $scope.parentProperty = 'parent scope';
}
function ChildCtrlFunction($scope) {
    $scope.childProperty = 'child scope';
    //just like in the DOM, we can access any of the properties in the
    //prototype chain directly from the current $scope
    $scope.fullSentenceFromChild = 'Same $scope: We can access: ' +
    $scope.rootProperty + ' and ' +
    $scope.parentProperty + ' and ' +
    $scope.childProperty;
}  

for Eg. https://github.com/shekkar/ng-book/blob/master/7_beginning-directives/current-scope-introduction.html

It is simple flow ,we have rootScope,parentScope,childScope .in each section we are assigning the corresponding scope variables.we can access the $rootScope in parentScope, rootScope and parentScope in childScope.

4 Comments

what's the point in adding a new answer to a question that was answered and accepted almost a year ago?
I would like to share my knowledge ,in this eg it explains the rootScope to parentScope and parentScope to childScope and url explains compete html code , i hope this may helpful for @Tristan and others
Thanks Shekkar, Please explain the meaning of your code snippet. A code snippet by itself is not helpful.
I think this is the most correct answer since it describes how to solve the actual problem. Thank you @Shekkar!

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.