title: angular wait for variable is defined
body: <p>I have 2 controllers and one service:</p>
  
  <pre><code>angular.module('objDescApp', ['ui.router', 'ui.bootstrap']);
  angular.module('objDescApp').config(['$stateProvider', '$urlRouterProvider', '$httpProvider', function ($stateProvider, $urlRouterProvider, $httpProvider) {
                  'use strict';
                  $urlRouterProvider.otherwise('/');
  
                  $stateProvider
                  .state('object', {
                     url: '/{name}',
                     views: {
                          "body": {
                              controller: 'ObjectDetailCtrl',
                              template: '...'
                          }
                  });
  
  
  angular.module('objDescApp').controller('ObjectListCtrl', function ($scope, $state, ConfigService) {
    ConfigService.getConfig(function(){//get config from server
     $scope.object = ConfigService.fillConfigForObjList(); //use config
    }
  }
  
  angular.module('objDescApp').controller('ObjectDetailCtrl', ['$scope', '$stateParams', 'ConfigService',  function ($scope, $stateParams, ConfigService) {
     $scope.current_object = ConfigService.fillConfigForObjDetail(); //use config
  }
  
  angular.module('objDescApp').factory('ConfigService', function ($http, $rootScope) {
    var jsonConf;
    var confTemplate = {"sometemplate" : {}};
  
    function fillConfigForObjList (){
       ... //use jsonConf variable , and always wait for init because of called only inside callback function of getConfig();
    };
    function fillConfigForObjDetail(){
      ... //use jsonConf variable , but doesnt wait for jsonConf  initialization, so error var 'is undefined' here.So I need to add some waiting for 'jsonConf' initialization logic here
  
     };
  
     return {
                jsonConf: jsonConf,
                fillConfigForObjDetail: fillConfigForObjDetail,
                fillConfigForObjList: fillConfigForObjList,
                getConfig: function(callback){           
                              $http({
                                method: 'GET',
                                url: endPointUrl,
                                transformResponse: undefined
                              }).then(
                                  function successCallback(response) {
                                      jsonConf = JSON.parse(response.data);
                                      $rootScope.getConfigError = false;
                                      callback();
                                  },
                                  function errorCallback(response) {
                                      if(response.status == "404"){
                                          jsonConf = confTemplate;
                                      }else{
                                          console.log("Get config error");
                                          jsonConf = confTemplate;
                                          $rootScope.getConfigError = true;
                                      }
                                      callback();
                                  }
                              );
                          }
      }
  </code></pre>
  
  <p>So, when I load page with main path <strong>'/'</strong> everything is OK, because '<em>ObjectListCtrl</em>' controller triggers <em>getConfig()</em> function which sets '<em>jsonConf</em>' variable after response, so I can naviagte between any states and all works fine cause 'jsonConf' already setted;</p>
  
  <p>But If I re-load page with starting path state like <strong>'/{name}'</strong> , so '<em>ObjectListCtrl</em>' controller trigers <em>'getConfig()'</em> request to server , but in async way  <em>'ObjectDetailCtrl'</em> controller was triggered and its <em>$scope.current_object = ConfigService.fillConfigForObjDetail()</em> expression, which throw <code>jsonConf is undefined error;</code>
  So could someone tell me how I can wait inside 'fillConfigForObjDetail()' function till 'jsonConf' variable init by getConfig() function.</p>