Vinicius Ataide
Vinicius Ataide

Reputation: 303

$q not returning the promise that I've sent

I have an issue that is taking out my patience. My promises started not working and I think it maybe a version issue for me. I've made this code several times and it worked like a charm:

angular.module('blogApp')
  .factory('person', function ($http, $q) {
    var url = 'http://localhost:3000/pessoas';

    return {
      getPeople: function() {
      var d = $q.defer();

      $http.get(url)
        .success(function(data) {
          d.resolve(data);
        });
      return d.promise;
    }
});

my controller:

angular.module('blogApp')
 .controller('MainCtrl', function ($scope, person) {        
   $scope.people = person.getPeople();
 });

I've logged person.getPeople() and its showing the deffered object without resolve.

What did I miss? Thanks!

Upvotes: 0

Views: 76

Answers (1)

Mik378
Mik378

Reputation: 22171

You are affecting a promise to your people variable. Whether it is resolved or not is not part of the issue. What you want is to affect the result of the http call, not a promise.

Here an example:

angular.module('blogApp')
  .factory('person', function ($http, $q) {
    var url = 'http://localhost:3000/pessoas';

    return {
      getPeople: function() {
         return $http.get(url);
    }
});

angular.module('blogApp')
 .controller('MainCtrl', function ($scope, person) {        
   person.getPeople()
       .then(function(data){
             $scope.people = data; 
        }
 });

Upvotes: 2

Related Questions