Html 如何在组件视图中访问rootscope
我有angular controller dashboard,在dashboard中设置根范围值,并使用scope在dashboard模板内的组件中访问该根范围值。但我不知道这是否正确。我无法获取该值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
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..初始化后,我也将被取消定义。你们能在这里提供工作代码片段吗?以便我能得到更好的想法,并为您提供有效的解决方案。谢谢。至少我需要这么做。