控制器内的角度解析 [英] Angular resolve within a controller
本文介绍了控制器内的角度解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的世界/主控制器我加载了几个ngResources。我怎样才能暂停别的执行,直到装?类似路线的决心属性。
//的全球性的控制器中,我执行负载操作一次。
app.controller('FrontendCtrl',函数($范围,authService,sessionService){ //我在这里检查,如果用户有一个cookie,如果是从服务器上获取的信息来重新初始化会话
authService.updateUser(); 。$ $范围会= sessionService;});//验证服务
app.factory('authService',函数($ Q,验证,其他,sessionService){ //从服务器填充本地用户数据 - 一个查询跟着几个后续的 - 都是ngResources
VAR UpdateUser两个=功能(){
Auth.profile(NULL,函数(数据){
如果(数据){
$ q.all({
富:Other.foo({USER_ID:data.id})
巴:Other.bar({USER_ID:data.id})
})
。然后(功能(结果){
sessionService.user = _.merge(结果,数据);
});
}
});
};
});//在我看来 - 如果在装载UpdateUser两个功能的延迟这不起作用
{{$ session.user.name}}
解决方案
您需要利用回调序,让服务先找填充会话值,这里是code你:
//验证服务
app.factory('authService',函数($ Q,验证,其他){ //从服务器填充本地用户数据 - 一个查询跟着几个后续的 - 都是ngResources
返回{
UpdateUser两个:函数(回调){
Auth.profile(NULL,函数(数据){
如果(数据){
$ q.all({
富:Other.foo({USER_ID:data.id})
巴:Other.bar({USER_ID:data.id})
})
。然后(功能(结果){
//如果sessionService只有一个目的在此情况下
回调(sessionService);
});
}
});
};
}});
和在控制器:
//的全球性的控制器中,我执行负载操作一次。
app.controller('FrontendCtrl',函数($范围,authService){ //我在这里检查,如果用户有一个cookie,如果是从服务器上获取的信息来重新初始化会话
//更新:现在一个回调来获取会话数据
authService.updateUser(功能(数据){
。$ $范围会话=数据;
});
});
In my global/main controller I load a few ngResources. How can I pause execution of anything else until loaded? similar to the resolve property of routes.
// The "global" controller in which I perform one time on load actions.
app.controller('FrontendCtrl', function($scope, authService, sessionService) {
// Here I check if the user has a cookie and if so fetch info from server to reinit session
authService.updateUser();
$scope.$session = sessionService;
});
// Auth service
app.factory('authService', function($q, Auth, Other, sessionService) {
// Populate local user data from server - one query followed by a few subsequent ones - all are ngResources
var updateUser = function() {
Auth.profile(null, function(data) {
if(data) {
$q.all({
foo: Other.foo({ user_id: data.id }),
bar: Other.bar({ user_id: data.id }),
})
.then(function(results) {
sessionService.user = _.merge(results, data);
});
}
});
};
});
// In my view - this doesn't work if there is a delay in loading the updateUser function
{{ $session.user.name }}
解决方案
You need to make use of callbacks inorder to let the service go first to populate the session value, here is the code for you:
// Auth service
app.factory('authService', function($q, Auth, Other) {
// Populate local user data from server - one query followed by a few subsequent ones - all are ngResources
return {
updateUser: function(callback) {
Auth.profile(null, function(data) {
if(data) {
$q.all({
foo: Other.foo({ user_id: data.id }),
bar: Other.bar({ user_id: data.id }),
})
.then(function(results) {
//If sessionService is only an object in this case
callback(sessionService);
});
}
});
};
}
});
and in the controller:
// The "global" controller in which I perform one time on load actions.
app.controller('FrontendCtrl', function($scope, authService) {
// Here I check if the user has a cookie and if so fetch info from server to reinit session
//Update: Now a callback to fetch session data
authService.updateUser(function(data){
$scope.$session = data;
});
});
这篇关于控制器内的角度解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文