0

I need to understand "How can I access the value of $rootScope value defined in one module into other module?"

Below is my code :

Index.html

<!DOCTYPE html>
<html ng-app="myapp">

<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js">  </script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<script src="test.js"></script>
</head>

<div ng-controller="serviceDIController">
Service Values is : <b> {{sdiParam}} </b>  <br/>
Factory Values is : <b> {{fdiParam}} </b>  <br/>
Root Scope Values is : <b> {{rootParam}} </b>  
</div>
</html>

script.js

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

app.controller('serviceDIController',['$scope','$rootScope',
'testService','testFactory',function($scope, $rootScope,testService, testFactory) 
{

 $scope.sdiParam = testService.param;
 $scope.fdiParam = testFactory.fparam;
// $scope.rootParam = $rootScope.root;      // How to access "$rootScope.root" value defined in test.js in current module inside a controller?

}
]);

test.js

var testapp = angular.module("testModule", []);

  testapp.service('testService', function() {
  this.param = "Service Param1 DI";
});

testapp.factory('testFactory', function() {

  var fact = {};
  fact.fparam = "Fact Param1 DI";
  return fact;  
});

testapp.controller('testCtrl', ['$scope',
  function($rootScope) {
    $rootScope.root = "Root Scope Param1";
  }
]);

Live demo : http://plnkr.co/edit/X0aamCi9ULcaB63VpVs6?p=preview

Checked below example but did not work:

9
  • possible duplicate of AngularJS Modules/Scope Sharing Commented May 23, 2014 at 9:20
  • @Sergiu Paraschiv : I don't think this is the duplicate. I want to access the value of $rootScope instead of $scope in my example. Commented May 23, 2014 at 9:22
  • Same thing. Any $scope anywhere is part of a $rootScope tree. Read docs.angularjs.org/api/ng/service/$rootScope and docs.angularjs.org/guide/scope Commented May 23, 2014 at 9:23
  • Can you check my example. I am not able to access the value of $rootScope. Commented May 23, 2014 at 9:24
  • 1
    "Every application has a single root scope. All other scopes are descendant scopes of the root scope." - "application" means "module". $rootScope is the root scope of a module. A controller's $scope is a child of the module's, AKA the $rootScope. So no, by design you cannot share $rootScope between modules. You can however initialize it (stackoverflow.com/questions/18880737/…) and maybe set it to some value persisted in a service, as described in the duplicate. Commented May 23, 2014 at 9:30

2 Answers 2

3

Explicitly inject '$scope', not '$rootScope' in testCtrl, so a new scope is created just for that controller and passed as the first argument regardless of the name used for that argument.

Incorrect:

testapp.controller('testCtrl', ['$scope',
  function($rootScope) {
    $rootScope.root = "Root Scope Param1";
  }
]);

Correct:

testapp.controller('testCtrl', ['$rootScope',
  function($rootScope) {
    $rootScope.root = "Root Scope Param1";
  }
]);
Sign up to request clarification or add additional context in comments.

Comments

2

Here is your updated working Plunkr

Basically I prefer to use $scope.$root to prevent the injection of the $rootScope.

You have also set the testCtlr on the <head> tag, don't know if it was on purpose or not.

Comments

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.