Angular JS 在解析中解析 [英] Angular JS resolve in a resolve

查看:29
本文介绍了Angular JS 在解析中解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析中解析的意思是我希望能够首先解析用户,然后解析他的所有附加信息,例如地址、联系人、朋友.

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.

我目前拥有的:

.state('website', {
            url : '',
            abstract : true,
            resolve : {
                // Request the current signed in user >
                current_user : ['Auth', function(Auth){
                    return Auth.requestUser();
                }]
            },
            views : {
                'main' : {
                    templateUrl : 'template.html'
                }
            }
        })

我想做的是:

.state('website', {
            url : '',
            abstract : true,
            resolve : {
                // Request the current signed in user >
                current_user : ['Auth', function(Auth){
                    Auth.requestUser().then(function(){
                       // HERE I WANT TO RESOLVE OTHER RESOURCES
                       // WHAT WILL I RETURN HERE?
                    });
                }]
            },
            views : {
                'main' : {
                    templateUrl : 'template.html'
                }
            }
        })

我不确定这是否可行.

推荐答案

您可以将已解析"参数注入到其他解析函数中:

You can inject "resolved" parameters into other resolve functions:

resolve: {
  current_user: ['Auth', function(Auth){
    return Auth.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 value
     return Svc.getFriends(current_user);
  }],

  address: ["Svc", "current_user", function(Svc, current_user){
     return Svc.getAddress(current_user);
  }]
}

然后,在该状态的控制器中,您可以注入 current_userfriendsaddress 作为参数:

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){
   // ...
})

这篇关于Angular JS 在解析中解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆