Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access rootscope in component view

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

like image 426
Trent Avatar asked Oct 17 '16 06:10

Trent


People also ask

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()

What is alternative of rootScope in angular?

What is alternative of rootScope in Angular? In similar way, you can use use the sharedService inside any component & fetch the returing output and used inside your view. That's it. I found this solution as a good alternative of rootScope.

What is $scope and $rootScope?

Root ScopeAll 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.


1 Answers

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

like image 174
Valery Kozlov Avatar answered Sep 20 '22 06:09

Valery Kozlov