Angular factory returning a promise

后端 未结 4 565
我在风中等你
我在风中等你 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:38

    Fo ui-router this is easily done with having an application root state with at least this minimum definition

    $stateProvider
        .state('app', {
            abstract: true,
            template: '
    ' resolve: { settings: function($http){ return $http.get('/api/public/settings/get') .then(function(response) {return response.data}); } } })

    After this you can make all application states inherit from this root state and

    1. All controllers will be executed only after settings are loaded
    2. All controllers will gain access to settings resolved value as possible injectable.

    As mentioned above resolve also works for the original ng-route but since it does not support nesting the approach is not as useful as for ui-router.

提交回复
热议问题