Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何在组件视图中访问rootscope_Html_Angularjs_View_Scope_Rootscope - Fatal编程技术网

Html 如何在组件视图中访问rootscope

Html 如何在组件视图中访问rootscope,html,angularjs,view,scope,rootscope,Html,Angularjs,View,Scope,Rootscope,我有angular controller dashboard,在dashboard中设置根范围值,并使用scope在dashboard模板内的组件中访问该根范围值。但我不知道这是否正确。我无法获取该值 function DashBoardController($http, $window, $rootScope, apiurl, $scope, $location,$interval) { var ctrl = this; ctrl.$onInit = f

我有angular controller dashboard,在dashboard中设置根范围值,并使用scope在dashboard模板内的组件中访问该根范围值。但我不知道这是否正确。我无法获取该值

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

我正在从模板中获取未定义的变量。您可以通过
$root.varName

访问rootscope变量。请在第一次加载DashBoardController时初始化$rootscope.test=“test”。我需要getUser()函数@JigarPrajapatiYes中的$rootscope.user值,但首先,此时将加载主控制器,您应该初始化它,然后无论何时调用getUser()函数,它都会覆盖它的值。但最重要的是你们需要初始化它。@JigarPrajapati..初始化后,我也将被取消定义。你们能在这里提供工作代码片段吗?以便我能得到更好的想法,并为您提供有效的解决方案。谢谢。至少我需要这么做。