0

I am creating a service to get the values saved as "FirstName" in a Row "name", when save is ok i have the values in the database, but when i try to get this values i have something like this:

people:

-

All the values in the list empty but i don't know why this happen if it is a problem in the controller or in the service.

<script src="https://www.parsecdn.com/js/parse-1.3.2.min.js"></script>
<div ng-app="app">
  <ul ng-controller="MyCtrl">
    people
    <li ng-repeat="person in people">{{person}}</li>
  </ul>
</div>

<script type="text/javascript">
Parse.initialize("key", "key");
var People = Parse.Object.extend("People");
var peoples= new People();
peoples.set("name", "FirstName");

peoples.save(null),{
    success:function(peoples){
        peoples.save();
    }
}


var app = angular.module('app', []);

app.controller("MyCtrl", ["$scope", "PeopleService", function($scope, PeopleService){
  $scope.people = PeopleService.getPeople();
}]);

app.service("PeopleService", function($q){
  var people = null;

  return {
  getPeople: function(){
          var deferred = $q.defer();
          people = [];
          var queryObject = new Parse.Query(People);
          queryObject.find({
            success: function (results) {
              for (var i = 0; i < results.length; i++) {
                var result = results[i];
                people.push(result.get("name"));
              }
              deferred.resolve(people);
            },
            error: function (error) {
              deferred.reject(error);
            }
          });
          return deferred.promise;
        }
  }
});
</script>

1 Answer 1

1

You should set the $scope.people value from the success callback function of parse.com call.

Code

app.controller("MyCtrl", ["$scope", "PeopleService", function($scope, PeopleService){
  PeopleService.getPeople().then(function(data){ //success fn
     $scope.people = data
  },function(data){ //error fn
     $scope.people = data
  });
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

after set the $scope.people with the code , i am having this error. Error: [ngRepeat:dupes]
@alsuelo use track by $index inside ng-repeat like <li ng-repeat="person in people">{{person}}</li>
works perfect, i want to ask you if you know how to create a service to save, edit and delete. I'll really appreciate it.

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.