javascript - Defer whole page load in AngularJS until service's ajax has finished - Stack Overflow

I have a single configService in my AngularJS project that fetches some configuration valuesof the wh

I have a single configService in my AngularJS project that fetches some configuration values of the whole project from the server via an ajax request, i.e. like whether or not user's need to be moderated before their account is activated.

To display information according to the configuration, the whole first page load should be deferred until this ajax request pleted. My service looks like:

angular.module('clientApp').factory('configService', function ($http) {
    var configService = {};
    var conf = {};

    Object.defineProperty(configService, 'serverConfig', {
        get: function () {
            return conf;
        }
    });

    $http.get('/api/config').success(function (data) {
        conf = $.extend(conf, data);
    });

    return configService;
});

So, since the service is a singleton, this will only be executed one time when the page loads, not on every route change.

Now I know how to use $q and promises, but my problem is how can defer the execution of ALL of angular until this service has finished its request? Most of my views will require values from configService.serverConfig and depend on it for specific behaviour - doing this asynchronously and have a defered.then() in every controller does not seem like the best idea.

I have a single configService in my AngularJS project that fetches some configuration values of the whole project from the server via an ajax request, i.e. like whether or not user's need to be moderated before their account is activated.

To display information according to the configuration, the whole first page load should be deferred until this ajax request pleted. My service looks like:

angular.module('clientApp').factory('configService', function ($http) {
    var configService = {};
    var conf = {};

    Object.defineProperty(configService, 'serverConfig', {
        get: function () {
            return conf;
        }
    });

    $http.get('/api/config').success(function (data) {
        conf = $.extend(conf, data);
    });

    return configService;
});

So, since the service is a singleton, this will only be executed one time when the page loads, not on every route change.

Now I know how to use $q and promises, but my problem is how can defer the execution of ALL of angular until this service has finished its request? Most of my views will require values from configService.serverConfig and depend on it for specific behaviour - doing this asynchronously and have a defered.then() in every controller does not seem like the best idea.

Share Improve this question asked Sep 2, 2013 at 12:47 DynalonDynalon 6,81410 gold badges58 silver badges87 bronze badges 1
  • 1 No, only a workaround (which is don't rely on the config options to be available at page load). An Upvote might increase the chance :) – Dynalon Commented Sep 24, 2013 at 13:41
Add a ment  | 

2 Answers 2

Reset to default 3
<html ng-app="yourApp">

angular.element(document).ready(function() {
  angular.bootstrap(document, ["yourApp"]);
});

maybe bootstrapping app manually can help...?

if that is not the case,then check this post ! Delaying AngularJS route change until model loaded to prevent flicker

I have written an angular module which emits a rootScope 'ajaxComplete' event once all initial ajax requests have been pleted.

It uses an angular interceptor which resets a timer when new request are send, and also tracks the number of pending requests. Then considers the initial ajax requests pleted once all responses return and no new requests are sent for 500 milliseconds. There is an example in the git project.

Happy coding.

https://github./jcarras/angular-ajax-plete

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744910372a4600512.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信