10

I have angular controller dashboard and set the root scope value in dashboard and access that root scope value in component inside the dashboard template using scope..but i don't know whether this is correct or not..and am not able to get that value

function DashBoardController($http, $window, $rootScope, apiurl, $scope, $location,$interval) {
        var ctrl = this;    
        ctrl.$onInit = function () {
            getUser();
        };    
        function getUser(){
            $http({
                method: 'GET',
                url: apiurl + '/getUser',
            }).success(function (data, status) {
                if (data.status == true) {
                    $rootScope.user  = data.result; 
                    $rootScope.test = "TEST";

                }  
            });
        }  

function Controller1($rootScope,$scope, $timeout,$http, apiurl,$interval) {
        var ctrl = this;    
         $scope.value = $rootScope.test;
   alert($scope.value);
       $scope.value1 = $rootScope.user;
   console.log($scope.value1);
}
    app.module('app').component('component1', {
        templateUrl: '../resources/views/component/component1.html',
        controller: Controller1
    });
})(window.angular);

am getting undefined

Trent
  • 105
  • 3
  • 12
  • Please initialize $rootScope.test = "TEST" , when DashBoardController load at first time. – Jigar7521 Oct 17 '16 at 06:41
  • I need $rootscope.user value from getUser() function @JigarPrajapati – Trent Oct 17 '16 at 06:44
  • Yes but first of all at that time your main controller will load, you should initialize it, then after whenever your getUser() function will call, it will overwrite it's value. But main thing is you need to initialize it. – Jigar7521 Oct 17 '16 at 06:49
  • @JigarPrajapati..after initialize also am getting undefined – Trent Oct 17 '16 at 06:58
  • Can you please provide working code snippet here? so that i can get better idea, and provide you a effective solution. – Jigar7521 Oct 17 '16 at 07:37
  • Because i have no idea, when you are calling Controller1 controller. – Jigar7521 Oct 17 '16 at 07:37
  • getUser() is defined in your DashBoardController, and you tell me, as you are never calling this controller at any moment, so how can root scope will get initialize? – Jigar7521 Oct 17 '16 at 07:40
  • Is Controller1 is called after DashBoardController? – RIYAJ KHAN Oct 17 '16 at 08:24
  • @RIYAJKHAN ...controller1 is component bind with dashboard page – Trent Oct 17 '16 at 08:41
  • @JigarPrajapati.. i want to store the $rootScope.user = data.result; after the getUser function call ...and same time need to access that in my controller1. console.log($rootScope) i can able to see the value,but accessing its undefined – Trent Oct 17 '16 at 08:53
  • Then you need utilize a then method of angular, this will allow you to wait until your function will return response and then you can initialize it. – Jigar7521 Oct 17 '16 at 08:55
  • @JigarPrajapati ..how to initialize that method...i'm not aware of that about angular – Trent Oct 17 '16 at 09:04
  • let me to make one code snippet for you by posting an answer. – Jigar7521 Oct 17 '16 at 09:16
  • @JigarPrajapati.,. I don't know how to give code snippet – Trent Oct 17 '16 at 13:50

1 Answers1

28

From template you can access rootscope variables via $root.varName

Valery Kozlov
  • 1,557
  • 2
  • 11
  • 19