仅在AngularJS中使用angular-global-resolve完成初始化后运行控制器

时间:2016-11-18 19:32:47

标签: javascript angularjs node.js angular-ui-router

我的应用程序初始化之前我的控制器正在运行时出现问题,我遇到了this NPM程序包几乎完美无缺。我唯一的问题是初始化函数在应用程序启动时几乎同时运行两次 以下是相关代码:

app.config(function($stateProvider, $urlRouterProvider, $locationProvider, globalResolveProvider){

  $urlRouterProvider.otherwise('/venues/feed');


  globalResolveProvider.addGlobalDependenciesTo($stateProvider, {
    getGlobalDependacies: function ($http, $rootScope, $cookies) {
      'ngInject';
      if ($rootScope.apiUrl){
        return
      }
      console.trace('do stuff')
        return $http.get('/__/env.json')
        .then(function(response) {
          $rootScope.apiUrl = response.data.apiUrl;
          $rootScope.googleMapsApiKey = response.data.googleMapsApiKey;
          $rootScope.currentLocationLat = 40.7589;
          $rootScope.currentLocationLng = 73.9851;
        })
        .then(function(){
          hotelId = ''
          if ($cookies.get('hotel') === undefined){
            $http.get($rootScope.apiUrl + '/hotels')
            .then(function(dbHotels){
              hotelId = dbHotels.data[0]._id
              $cookies.put('hotelId', hotelId)
            })
          }
          if ($cookies.get('userId') === undefined){
            $http.get($rootScope.apiUrl + '/users')
            .then(function(dbUsers){
              index = dbUsers.data.length - 1
              userId = dbUsers.data[index]._id
              $cookies.put('userId', userId)
              $rootScope.$broadcast('update-itinerary-icon')
            })
          }
        })
    }
  })

  $stateProvider
  .state('venues', {
    url: '/venues',
    abstract: true,
    template:'<div ui-view></div>'

  ....more states....

  $locationProvider.html5Mode(true);
});

关于为什么这可能会运行两次的任何想法?

0 个答案:

没有答案