html - 如何在组件 View 中访问根作用域

标签 html angularjs view scope rootscope

我有 Angular Controller 仪表板并在仪表板中设置根范围值并使用范围访问仪表板模板内组件中的根范围值..但我不知道这是否正确..我不能获得那个值(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);

我变得不确定了

最佳答案

从模板中,您可以通过 $root.varName 访问 rootscope 变量

关于html - 如何在组件 View 中访问根作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40079835/

相关文章:

javascript - 计算父 div 中的子项并通过单击按钮直到最后一个子项删除每个子项

javascript - 使用 Angular 循环遍历 json 数据并填充缺失值

mysql - 使用连接创建 View

view - iOS 8 - Swift - Xcode 6 Beta - 在没有界面生成器的情况下工作(没有 Nib 和 Storyboard)

javascript - 使用 ng-repeat 创建 DOM 属性

java - Android 以编程方式添加 View 重复索引

html - iOS UIWebView 不缩放内容

javascript - 在范围输入的缩略图内放置一个值

css - 将水平列表放在 div 的底部边界

javascript - Angular $http POST 无法绑定(bind)数组