AngularJS: Call a particular function before any partial page controllers

后端 未结 3 1042
青春惊慌失措
青春惊慌失措 2021-02-08 03:19

I want to call a particular function: GetSession() at the beginning of my application load. This function makes a $http call and get a session token: <

3条回答
  •  星月不相逢
    2021-02-08 04:18

    You have not provided any details related to GetSession. For scenarios like this you should use the resolve property while defining your routes in $routeProvider. I see you are using resolve already.

    What you can do now is to wrap the GlobalSessionToken into a Angular service like GlobalSessionTokenServiceand call it in the resolve to get the token before the route loads. Like

    resolve: { 
                loadData: function($q){ 
                    return LoadData2($q,'home'); 
                },
                GlobalSessionToken: function(GlobalSessionTokenService) {
                     return GlobalSessionTokenService.getToken()  //This should return promise
                }
             } 
    

    This can then be injected in your controller with

    controllers.MasterController = function($rootScope, $http,GlobalSessionToken){

提交回复
热议问题