Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print $rootScope variable?

This code is not working:

<td><img src="{{$rootScope.s3BucketUrl}}"/></td>

and this is where it's defined:

var appCtrl = app.controller('AppCtrl', function($scope, $resource, $location, $route, sharedProperties, $q, $rootScope){
    defer = $q.defer();
    //$rootScope.s3BucketUrl = 'http://lynd.s3.amazonaws.com/';//comment below one when live
    $rootScope.s3BucketUrl = 'http://lynd-test.s3.amazonaws.com/';
//code omitted
});

AppCtrl is bound first on body tag.

Could anybody please suggest how do I print $rootScope variable?

like image 529
Jack Avatar asked Jul 20 '13 06:07

Jack


People also ask

How do I get the scope value in console?

scope(); $('#elementId'). scope(). $apply(); Another easy way to access a DOM element from the console (as jm mentioned) is to click on it in the 'elements' tab, and it automatically gets stored as $0 .

What is $rootScope in JavaScript?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.

What is the difference between $scope and $rootScope?

The main difference is the availability of the property assigned with the object. A property assigned with $scope cannot be used outside the controller in which it is defined whereas a property assigned with $rootScope can be used anywhere.


1 Answers

Just use {{s3BucketUrl}} and you would get the value.

Your current scope is a child scope of rootscope and hence you can always access the elements of rootscope.

This wiki is highly recommended

like image 147
Chandermani Avatar answered Oct 26 '22 10:10

Chandermani