0

I have an angularjs app with 2 main folder. Webapp -> admin / user.

Each one contain html and js files in order to display the webapp. All the app. is based on REST principal, so when I get the data from the backend (springMVC/Hibernate/JPA), it's a JSON file.

On the admin side, I have a user.js file with a controller :

Service.userRead($routeParams.id).then(function(data) {
     if (data.status == SUCCESS) {
           $scope.userIn = data.user;
           var color = $scope.userIn.bannerColor;

     } else {
           logger.warn('userRead: ' + JSON.stringify(data));    
           $scope.redirectTo('/users');
     }
}

On the user side, when the client is on the webapp (not connected to the admin side) I want to obtain the background-color previously stored for a particular user in the database. these hex code is for styling the header.

the question is : how can I retrieve data from the controller user.js and past the variable color to the header controller located in the other folder (HeaderController in user side) ?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
JohnCooper
  • 15
  • 2
  • 10

1 Answers1

0

Refer AngularJS Modules/Scope Sharing

Community
  • 1
  • 1
Gayathri
  • 1,776
  • 5
  • 23
  • 50
  • thanks @Gayathri for your answer. but when I made it, on the other controller for console.log($rootScope.variable) I got "undefined". – JohnCooper Jan 17 '17 at 14:57
  • Are both the controllers available in same module? Perhaps some more portion of code can help me identify why – Gayathri Jan 17 '17 at 15:04
  • Try module.controller('MyController', ['$scope', '$rootScope',function($scope, $rootScope) { ... }]); – Gayathri Jan 17 '17 at 15:06
  • controllers are available in same module. Declared separately in two index.js as : var myApp = angular.module('myApp', ['ngAnimate', 'ngRoute', 'ngSanitize', 'angular-growl', 'angulartics', 'angulartics.google.analytics', 'myAppControllers']); – JohnCooper Jan 17 '17 at 15:16
  • and then : var controllers = angular.module('myAppControllers', []); in order to simplify the declaration of a controller in user.js controller for example – JohnCooper Jan 17 '17 at 15:25
  • declaration of the HeaderController : controllers.controller('HeaderController', [ '$scope', '$rootScope', '$timeout', 'myAppService', function($scope, $rootScope, $timeout, myAppService) { ... }]); – JohnCooper Jan 17 '17 at 15:26
  • Hi, I'm sorry. Actually $rootScope actually work within different controllers of one single app,but not two different app. I have shared a link in my answer which matches yours. – Gayathri Jan 17 '17 at 16:09