1

I am having a problem where my controllers are running before initialization of my app, and I came across this NPM package that works almost perfectly. The only problem I'm having is the initialization function runs twice at almost the same time when the app starts.
Here is the relevant code:

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);
});

Any Idea on why this might be running twice?

1
  • Did you ever resolve this? I'm having the same problem now. Mine is even running three times! Commented Jun 28, 2017 at 10:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.