Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the $rootScope be accessed in the template of a directive with isolate scope?

With isolate scope the template of the directive does not seem to be able to access the controller ('Ctrl') $rootScope variable which, however, does appear in the controller of the directive. I understand why the controller ('Ctrl') $scope variable isn't visible in the isolate scope.

HTML:

<div ng-app="app">     <div ng-controller="Ctrl">         <my-template></my-template>     </div>      <script type="text/ng-template" id="my-template.html">         <label ng-click="test(blah)">Click</label>     </script> </div> 

JavaScript:

angular.module('app', [])     .controller('Ctrl', function Ctrl1($scope,  $rootScope) {         $rootScope.blah = 'Hello';         $scope.yah = 'World'     })     .directive('myTemplate', function() {         return {             restrict: 'E',             templateUrl: 'my-template.html',             scope: {},             controller: ["$scope", "$rootScope", function($scope, $rootScope) {                 console.log($rootScope.blah);                 console.log($scope.yah);,                  $scope.test = function(arg) {                     console.log(arg);                 }             }]         };     }); 

JSFiddle

The variable is accessed with no isolate scope - as can be seen by commenting the isolate scope line:

        // scope: {}, 
like image 264
camden_kid Avatar asked May 11 '14 17:05

camden_kid


People also ask

What is $rootScope and how does it relate to $scope?

"$rootScope” is a parent object of all “$scope” angular objects created in a web page. $scope is created with ng-controller while $rootscope is created with ng-app . Show activity on this post. The main difference is the availability of the property assigned with the object.

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.

How many $rootScope an angular application can have?

HTML. The scope in AngularJS is hierarchical in nature: The $rootScope acts as a global variable. All the $scopes of an AngularJS application are children of the $rootscope. An app can have only one $rootScope.

What is rootScope apply ()?

$scope.$apply() This function is used to execute an expression in Agular. The function expression is optional and you can directly use $apply(). This is used to run watcher for the entire scope. $rootScope.$digest()


Video Answer


1 Answers

You can try this way out using $root.blah

Working Code

html

 <label ng-click="test($root.blah)">Click</label> 

javascript

  angular.module('app', [])     .controller('Ctrl', function Ctrl1($scope,  $rootScope) {         $rootScope.blah = 'Hello';         $scope.yah = 'World'     })     .directive('myTemplate', function() {         return {             restrict: 'E',             templateUrl: 'my-template.html',             scope: {},             controller: ["$scope", "$rootScope", function($scope, $rootScope) {                 console.log($rootScope.blah);                 console.log($scope.yah);                  $scope.test = function(arg) {                     console.log(arg);                 }             }]         };     }); 
like image 179
Nidhish Krishnan Avatar answered Oct 11 '22 09:10

Nidhish Krishnan