N
N
Nick Nesterov2015-12-17 14:31:15
Angular
Nick Nesterov, 2015-12-17 14:31:15

Resolve after .success?

Good time of the day.
Please tell me how to load the controller only after the response comes from the server.
Our App.js

...
$routeProvider.when('/', {
      templateUrl: 'views/home.html',
      controller: 'HomePageCtrl',
         resolve: // что необходимо вставить?
...

Our controller
angular.module('someApp').controller('HomePageCtrl', ['$scope', '$http', function($scope, $http){
  $http.get("/data").success(function(data){
                       $scope.data = data;
        });
}]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2015-12-17
@njnesterov

The resolver must receive a Promise object.
For example:

resolve: {
    data: ['loader', function(loader) {
        // метод, выполняющий запрос к серверу
        return loader.load();
    }],
}

UPD:
...
$routeProvider.when('/', {
      templateUrl: 'views/home.html',
      controller: 'HomePageCtrl',
         resolve: 
         	{
         		loadedContent: ['$http', function($http) {
         			return $http.get('/data');
         		}],
         	}
...

Now we simply inject loadedContent into the controller, and Angular will pass there what the Promise returns to us.
angular.module('someApp').controller('HomePageCtrl', ['$scope', 'loadedContent', function($scope, loadedContent){
  $scope.data = loadedContent;
}]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question