AngularJS - 无法访问RootScope [英] AngularJS - Cannot access RootScope
问题描述
奇怪的问题,
我无法访问在在CTRLA越来越设置CTRLB变量$ rootScope。
Strange problem, I cannot access the $rootScope in CtrlB variable that is getting set in CtrlA.
HTML
<div role="main" class="container_9" ng-controller="CountryCtrl" ng-init="updateToken('<?php echo $TOKEN; ?>')">
CTRLA
app.controller('CountryCtrl', function ($scope,$rootScope, $http) {
$scope.updateToken = function(token) {
$rootScope.token = token;
}
});
CTRLB
app.controller('DealerListCtrl', function ($scope, $http, $rootScope, dealerService) {
$scope.dealer = [];
$http.get('files/framework/dealer/'+ $rootScope.token).success(function(data) {
$scope.dealerall = data;
});
//$scope.dealerall = dealerService.api.get({token: $scope.token});
$scope.orderProp = 'name';
});
错误消息:
/framework/dealer/undefined 500 (Internal Server Error)
我是什么做错了吗?
What am I doing wrong?
更新结果
Plunker code:结果
http://plnkr.co/edit/r559zyMKjA64xSdmrTem
这是不能够运行...
It's not capable to run...
推荐答案
它访问 $ rootScope
就好了。如果不是你会得到约$ rootScope JavaScript错误没有被定义的。相反,它的套结不确定到请求的URL的结尾,这意味着 $ rootScope.token
是不确定的。做任何你需要确保 $ rootScope.token
定义。
It's accessing the $rootScope
just fine. If it wasn't you'd be getting a javascript error about $rootScope not being defined. Instead, it's tacking "undefined" onto the end of the request URL, meaning $rootScope.token
is undefined. Do whatever you need to to make sure $rootScope.token
is defined.
我怀疑CTRLB获取调用之前CTRLA,所以 $ rootScope.token
还没有确定呢。
I suspect CtrlB is getting called before CtrlA, and so $rootScope.token
hasn't been set yet.
这篇关于AngularJS - 无法访问RootScope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!