Angular和ParseSDK Javascript CRUD操作

时间:2015-08-13 19:53:50

标签: javascript angularjs parse-platform

我正在创建一个服务来将值保存为" FirstName"在一行"名称",当保存是好的我有数据库中的值,但当我尝试得到这个值我有这样的东西:

人:

-

列表中的所有值都为空,但我不知道为什么在控制器或服务中出现问题时会发生这种情况。

    
<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 个答案:

答案 0 :(得分:1)

您应该从parse.com来电的成功回调功能中设置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 }); }]); 值。

<强>代码

{{1}}