简体   繁体   English

用户登录后获取用户个人资料

[英]Getting user profile in angular after user logged in

In my application each user has a specific profile, I have created a service to hold this profile named currentUser . 在我的应用程序中,每个用户都有一个特定的配置文件,我创建了一个服务来保存名为currentUser配置文件。

I use the properties of this profile to render my the header of site and make further api calls. 我使用此配置文件的属性来呈现网站的标题并进行进一步的api调用。

the problems is that when should I fill this currentProfile service? 问题是我何时应该填写此currentProfile服务?

There are a few options: 有几种选择:

1 - after login promise is resolved: 1-解决登录承诺后:

authService.login(username, pass)
    .then(function(response) {
        userService.getProfile()
            .then(function(profile){
                currentUser.setProfile(profile);
                $state.go('dashboard');
            });
    });

the main problem with this approach is that if a user is in this url /main/list and refreshes the page, currentUser gets emptied. 这种方法的主要问题是,如果用户位于此url /main/list并刷新页面,则currentUser将被清空。

2 - using ui-router deferIntercept and urlRouter.sync() and $urlRouter.listen() like here , but the main problem with this approach is that my header directive and controller gets executed before (or during) event handler of $locationChangeStart and currentUser is not filled yet. 2-像这里一样使用ui-router deferIntercepturlRouter.sync()$urlRouter.listen() ,但是这种方法的主要问题是我的标头指令和控制器在$locationChangeStartcurrentUser尚未填写。

How can I achieve my desried effect? 我怎样才能达到预期效果? any idea is appreceated! 任何想法都得到体现!

Edit : I don't want to rely on localStorage or cookie or .... 编辑 :我不想依靠localStorage或cookie或...。

Use ui-router , it has a resolve property which ensures data is loaded before the application continues. 使用ui-router,它具有resolve属性,可确保在应用程序继续之前加载数据。

 $stateProvider .state('main.list', { url: "/main/list", templateUrl: "views/custom/index.html", controller:MainController, resolve: { loginUser : function($q){ //load data or whatever return loadData(); } } }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM