Angular factory returning a promise

后端 未结 4 558
我在风中等你
我在风中等你 2020-12-08 09:40

When my app starts I load some settings from a server. Most of my controllers need this before anything useful can be done. I want to simplify the controller\'s code as much

4条回答
  •  时光说笑
    2020-12-08 10:33

    You can manually bootstrap your application after settings are loaded.

    var initInjector = angular.injector(["ng"]);
    var $http = initInjector.get("$http");
    var $rootScope = initInjector.get("$rootScope");
    
    $http.get('/api/public/settings/get').success(function(data) {
        $rootScope.settings = data;
        angular.element(document).ready(function () {
            angular.bootstrap(document, ["app"]);
        });
    });
    

    In this case your whole application will run only after the settings are loaded.

    See Angular bootstrap documentation for details

提交回复
热议问题