Angular Js Resolve In A Resolve
What I mean by resolve in a resolve is that I want to be able to resolve the user first, and then resolve all his additional info, eg, addressess, contacts, friends. What I current
Solution 1:
You can inject "resolved" parameters into other resolve functions:
resolve: {
current_user: ['Auth', function(Auth){
returnAuth.requestUser();
}],
// "Svc" here is some service I assume you have to get friends, address, etc...friends: ["Svc", "current_user", function(Svc, current_user){
// Svc.getFriends can return a promise or a valuereturnSvc.getFriends(current_user);
}],
address: ["Svc", "current_user", function(Svc, current_user){
returnSvc.getAddress(current_user);
}]
}
Then, in the controller for that state, you can inject current_user
, friends
, and address
as parameters:
.controller("WebsiteMainViewCtrl", function($scope, current_user, friends, address){
// ...
})
Solution 2:
You can do something like this.
resolve : {
// Request the current signed in user >
current_user : ['Auth', function(Auth){
var deferred = $q.defer();
Auth.requestUser().then(function(){
// Call this deferred.resolve in inner asyc callback
deferred.resolve(data);
});
return deferred.promise;
}]
}
Or
You can also use chaining
resolve : {
// Request the current signed in user >
current_user : ['Auth', function(Auth, userInformation){
var authData;
return Auth
.requestUser()
.then(function (_authData) {
// do something with dataFromAuth
authData = _authData;
return userInformation.getDetailInfoAboutUser(_authData.userid);
})
.then(function (detailedUserInfoObject) {
// do something with dataFromOtherResource and return
detailedUserInfoObject.authInfo = authData;
return detailedUserInfoObject;
});
}]
}
Post a Comment for "Angular Js Resolve In A Resolve"