Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access $rootscope variable in angularjs?

I want to assign some data to a global variable (may be $rootscope) which gets initialized at pageload. And then I want to access that data from all the controllers. How would I do that?

// Edit : I tried following and it works as expected....

function Ctrl1($rootScope) {
    $rootScope.value = "asdf";
}

function Ctrl2($rootScope, $scope) {
    $scope.value = $rootScope.value;
}
like image 558
exAres Avatar asked Nov 30 '25 14:11

exAres


2 Answers

MyController($rootScope,$scope)
{
  $rootScope = ....
}

you get the point :)

like image 194
CodeSamurai777 Avatar answered Dec 07 '25 00:12

CodeSamurai777


If you are trying to show the variable in the view, there is not need of assign the variable from $rootScope to $scope.

like image 37
julian Avatar answered Dec 06 '25 22:12

julian