角度回调或承诺回收谷值

时间:2016-06-09 21:25:10

标签: angularjs callback angular-promise

我正绞尽脑汁试图让它正常工作。我尝试过回调和承诺,对两者都持开放态度。我使用由Joe Eames在Pluralsight类中布局的Mean Stack建模我的代码。我还在学习,请原谅我的术语,如果不正确,但这是如何使用代码,如下图所示。

我的接口调用flQuestionCrudCtrl.createQuestion调用调用flQuestion.Create的flQuestionCrud.createNewQuestion。 flQuestion.Create向写入mongodb集合的服务器发出http post请求。一切似乎工作正常,数据写入数据库,我可以在每个控制台日志的flQuestionCrud中看到返回值。当我尝试读取flQuestionCrudCtrl中的“data”返回参数时,我得到“错误:数据未定义”。我已经尝试了回调和承诺的每一个组合,但无法让它工作。你能看出我做错了吗?

  angular.module('app').controller( 'flQuestionCrudCtrl',function(flConstructDataService, flQuestionDataService, flQuestionCrud, flCachedReferenceData,flCachedQuestions, flAnswer, $scope, $location, $q, flIdentity, flNotifier,$timeout, $state) {


$scope.createQuestion = function() {

function doWorkAsync() {
    return $timeout(flQuestionCrud.createNewQuestion(currentQuestionData), 10);
}

doWorkAsync()
    .then(function(data) {
        console.log("flQuestionCrudCtrl - Success " + data);
        console.log("flQuestionCrudCtrl - Success Statement " + data.statement);
        console.log("flQuestionCrudCtrl - Success Question id " + data._id);
        currentQuestionData._id = data._id;
        flQuestionDataService.setNewQuestionId(data._id);
        console.log("flQuestionCrudCtrl - currentQuestionData._id " + currentQuestionData._id);
        console.log("flQuestionCrudCtrl - flQuestionDataService.getNewQuestionId() " + flQuestionDataService.getNewQuestionId());
        $state.go('questionUpdate');
    })
    .catch(function(err) {
        console.log("flQuestionCrudCtrl - Error " + err);
        $state.go('questionCreate');
    })
    .finally();
};

}


angular.module('app').factory('flQuestionCrud',function($http, $q, $state, $timeout, flQuestion ){
    return {
        createNewQuestion: function(newQuestionData) {
            console.log("Before - flQuestion.create");

        function createQuestionDataAsync(questionData) {
            console.log("flQuestionCrud - Before Call to create ")
            var returnData;
            flQuestion.create(questionData, function(data) {
                console.log("flQuestionCrud - After Call to create ")
                if (!data){
                    return Error("Error Creating Data");
                    //return null;
                }
                else {
                    console.log("flQuestionCrud - Try Section - Success " + data);
                    console.log("flQuestionCrud - Try Section - Success Statement " + data.statement);
                    console.log("flQuestionCrud - Try Section - Success Question id " + data._id);
                }
                return data;
            });
        }

        createQuestionDataAsync(newQuestionData); 
    },

1 个答案:

答案 0 :(得分:2)

您的flQuestionCrud.createNewQuestion()方法应返回使用data解决的承诺:

angular.module('app')
    .factory('flQuestionCrud', function($http, $q, $state, $timeout, flQuestion ){
        return {
            createNewQuestion: function(newQuestionData) {
                var deferred = $q.defer();

                flQuestion.create(questionData, function(data) {
                    if (data){
                        // whatever we resolve the promise with will be passed 
                        // to the `then` handler in the controller
                        deferred.resolve(data);
                    }
                    else {
                        deferred.reject("Error Creating Data");
                    }
                });

                return deferred.promise;
            }
            // ...
        }
    });

注意,我们明确创建承诺并根据flQuestion.create()返回的内容来解决或拒绝承诺。

这样您就可以按照自己的方式访问控制器中的data

flQuestionCrud.createNewQuestion(currentQuestionData)
    .then(function(data) {
        // work with data here
    });

doWorkAsync()
    .then(function(data) {
        // work with data here
    });