How to access rootscope in component view
Asked Answered
M

1

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

Marlomarlon answered 17/10, 2016 at 6:31 Comment(14)
Please initialize $rootScope.test = "TEST" , when DashBoardController load at first time.Alphorn
I need $rootscope.user value from getUser() function @JigarPrajapatiMarlomarlon
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.Alphorn
@JigarPrajapati..after initialize also am getting undefinedMarlomarlon
Can you please provide working code snippet here? so that i can get better idea, and provide you a effective solution.Alphorn
Because i have no idea, when you are calling Controller1 controller.Alphorn
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?Alphorn
Is Controller1 is called after DashBoardController?Deste
@RIYAJKHAN ...controller1 is component bind with dashboard pageMarlomarlon
@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 undefinedMarlomarlon
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.Alphorn
@JigarPrajapati ..how to initialize that method...i'm not aware of that about angularMarlomarlon
let me to make one code snippet for you by posting an answer.Alphorn
@JigarPrajapati.,. I don't know how to give code snippetMarlomarlon
B
28

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

Bodrogi answered 17/10, 2016 at 8:2 Comment(1)
Thank you. That's what I needed to do, at least.Occupant

© 2022 - 2024 — McMap. All rights reserved.