title: Angular JS delay controller and directive initialization until data is returned from server
body: <p>My AngularJS app requires some metadata from the server before it can initialize the controller and a handful of directives.   I have a service to return the data but have been unable to get the controller and directives to wait for the data. </p>
  
  <pre><code>myapp.factory("db",["$http",function($http)
  {
    var db = {};
  
    db.metadata = function(callback)
    {
      $http.jsonp("/db/?f=metadata&amp;callback=JSON_CALLBACK").success(function(data)
      {
        callback(data);
      }).error(function(error)
      {
        console.log(error);
      });
    }
    ...
    return db;
  }]);
  
  
  var myctrl = myapp.controller("MyCtrl",["$scope","$location","db",function($scope,$location,db)
  {
    db.metadata(function(data)
    {
      $scope.metadata = data;
      // initialize $scope members with $scope.metadata for directives
       ...
    });
  }]);
  </code></pre>
  
  <p>This obviously won't work since the directives will attempt to initialize using undefined $scope members before the call to db.metadata() returns.   I have tried using the $routeProvider resolve property:</p>
  
  <pre><code>myapp.config(["$routeProvider",function($routeProvider)
  {
    $routeProvider.when("/",{
      templateUrl: "./myview.html",
      controller: "MyCtrl",
      resolve: {
        metadata: ["db","$q",function(db,$q)
        {
          var deferred = $q.defer();
  
          db.metadata(function(data)
          {
            deferred.resolve(data);
          });
  
          return deferred.promise;
        }]
      }
    });
  }]);
  
  var myctrl = myapp.controller("MyCtrl",["$scope","$location","db","metadata",function($scope,$location,db,metadata)
  {
    $scope.metadata = metadata;
    // initialize $scope members with $scope.metadata for directives
    ...
  }
  </code></pre>
  
  <p>This doesn't run as the minification friendly declaration of "metadata" in the injection array causes Angular to puke.   Removing the declaration results in the injected metadata to be undefined.  What am I doing wrong?</p>