如何在组件视图中访问rootscope

时间:2016-10-17 06:31:44

标签: html angularjs view scope rootscope

我有角度控制器仪表板,并在仪表板中设置根范围值,并使用范围访问仪表板模板内部组件中的根范围值。但我不知道这是否正确......我是无法获得该值

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

我未定义

1 个答案:

答案 0 :(得分:14)

从模板中,您可以通过$root.varName

访问rootscope变量
相关问题