问题描述:

I have a 'userSessionDataService'. This fetches the user's details from the server (ONLY ONCE).

I want the user data to be accessible when the service is called, but without the need to call .then() on a promise.

From what I understand this is not supported by angular. The caller of the service must always use a promise to access data that were loaded asynchronously (e.g.through ajax).

网友答案:

You may find useful to manually bootstrap like I did for localisation:

$(function(){
    $.ajax({
        url: "/GetResources",
        type: "POST",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            var mn = 'app';
            angular.module(mn).value('Resources', result);
            angular.bootstrap(document, [mn]);
        }
    });
});

Resources are always available this way:

angular.module('app').run(function($rootScope, Resources){
     $rootScope.resources = Resources;
});
相关阅读:
Top