确保一个AngularJS控制器先于另一个加载
Ensuring one AngularJS controller loads before another
在开始这篇文章之前,我真的不知道这是否是实现我正在做的事情的最佳方式,我非常愿意接受更好的建议。
我有一个使用OAUTH2
的用户帐户系统,它可以在我的数据库中查找用户信息,并将其保存为变量$rootScope.userInfo
。这驻留在一个控制器中,该控制器附加到我的应用程序的body
;在这里,我认为最高级别的控制器会在其中的控制器之前加载,但显然不是。
如果我在mainCtrl
有机会从数据库加载之前加载了一个试图访问此$rootScope.userInfo
对象的视图,它会抛出一个javascript错误,Angular
会中断。
作为参考,这里有一个模板的大致想法:
<body ng-controller="mainCtrl">
<header>Content</header>
<div class='main-view' ng-controller="userProfile">
<p>{{user.name}}</p>
<p>{{user.email}}</p>
</div>
</body>
我正在mainCtrl
中加载$rootScope.userInfo
,如下所示:
$http.get('/api/users/' + $cookies.id).
success(function(data) {
$rootScope.userInfo = data.user[0];
console.log("User info is:");
console.log($rootScope.userInfo);
$scope.status = 'ready';
});
然后,对于我的userProfile
控制,我会:
function userProfile($scope, $cookies, $http, $routeParams, $rootScope){
if($scope.status == 'ready'){
$scope.user = $rootScope.userInfo;
console.log("Double checking user info, here it is:");
console.log($rootScope.userInfo);
}
}
如果我来自应用程序中不调用$rootScope.userInfo
的其他页面,API有足够的时间查找它,我的userProfile
页面工作正常。但是,如果进行全页刷新,$rootScope.userInfo
就没有时间加载,并且会出现错误。
我该怎么解决这个问题?
您描述的问题是不建议使用$rootScope
在控制器之间共享数据的原因之一:它在两个控制器之间创建了手动的"不可见"依赖关系,当最终用户还没有通过另一个控制器时,您必须手动修复该依赖关系。
建议的解决方案是将用户加载逻辑移动到一个服务中,比如userProfileService
,将其注入到两个需要它的控制器中。然后,它将被创建一次,并用于两个控制器。在这样的服务中,当控制器请求时,您可以用$http
加载用户配置文件,并在下一个请求时从缓存中返回。通过这种方式,依赖关系从两个控制器转移到共享服务,而不是从一个控制器转移至另一个控制器。
我不是AngularJS文档的超级粉丝,但这些文档可能会有所帮助:DI、创建服务和注入服务。
使用then
而不是成功,并使用ng-include
:延迟加载子控制器
<body ng-controller="mainCtrl">
<header>Content</header>
<ng-include src="templatePath"></ng-include>
</body>
在新模板userprofile.html
:中移动HTML
<div class='main-view' ng-controller="userProfile">
<p>{{user.name}}</p>
<p>{{user.email}}</p>
</div>
主目录中的回调Ctrl:
$http.get('/api/users/' + $cookies.id).
then(function(data) {
/* mainCtrl being the parent controller, simply exposing the
properties to $scope would make them available
to all the child controllers.*/
$rootScope.userInfo = data.user[0];
/* assign the template path to load the template and
instantiate the controller */
$scope.templatePath = "userprofile.html";
});
- 如何在Angular单元测试中从另一个控制器的rootScope将方法添加到rootScope中
- 从AngularJS中的另一个文件中的控制器访问服务
- 如何将一个变量从一个控制器传递到合金中的另一个控制器
- 如何从AngularJs中的另一个控制器访问控制器
- 相对于角度控制器中的另一个阵列过滤阵列项目
- 单击angularjs中的另一个控制器时重新加载控制器
- ASP.NET angularjs重定向到控制器的另一个视图
- 如何在来自另一个文件的控制器中定义变量
- Angularjs - 观察模态窗口从另一个控制器关闭
- 在一个控制器中更新服务变量,并在另一个控制器 - Angular JS中使用更新的值
- AngularJs 从指令中的另一个调用控制器
- 如何使用javascript express node将变量从控制器中的一个方法公开.js到另一个控制器.js
- 将数据从控制器传递到另一个控制器
- 如何从另一个控制器更新控制器的ng重复模型
- 从另一个控制器调用一个控制器
- 无法更改呼叫另一个控制器
- angularjs调用另一个控制器's函数
- 访问一个控制器的ng隐藏值以在全局控制器中操纵另一个控制器中的ng样式
- 无法使用typescript访问angular中另一个控制器中声明的$rootscope属性
- 角度-将数组中的信息从一个控制器传递到另一个控制器