2

这是基本代码。此代码将承诺传递给控制器​​,但我需要将结果对象返回给控制器。

openupServices.factory('getCurrentProject', 
function($rootScope, $q, $location, $resource) {
    var factory = {};
    factory.getProject = function () {
        var deferred = $q.defer();
        var AppProject = Parse.Object.extend("AppProject");
        var query = new Parse.Query(AppProject);
        query.equalTo("userId", $rootScope.sessionUser.id);
          query.find({
            success: function(results) {
                console.log(results);
          },
          error: function(error) {
            alert("Error: " + error.code + " " + error.message);
          }     
        });
        return deferred.promise;
    }
    return factory;
});


openupControllers.controller('ProjectCtrl',
    function($scope, $location, $rootScope, getCurrentProject) {
        var thisProject = getCurrentProject.getProject(); 
    });
4

1 回答 1

1

首先,您需要解决和拒绝服务中关于成功和错误的承诺。像这样:

query.find({
    success: function(results) {
        deferred.resolve(results);
    },
    error: function(error) {
        deferred.reject(error);
    }
});

然后在控制器中:

getCurrentProject.getProject().then(function(data) {
    $scope.thisProject = data;
});
于 2014-03-09T18:00:37.563 回答