This question already has answers here:
Is this a “Deferred Antipattern”?
                                
                                    (3个答案)
                                
                        
                        
                            Why are AngularJS $http success/error methods deprecated? Removed from v1.6?
                                
                                    (2个答案)
                                
                        
                                去年关闭。
            
                    
我试图等待服务器返回一些数据,然后执行另一个回调。这是我的代码

commonApp.factory('replyMessageFactory', ['$q', '$http', '$log', function ($q, $http, $log) {
var factoryObj = {};

factoryObj.send = function (obj) {
    var req = {
        method: 'POST',
        url: "/api/ThreadMessage",
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        },
        data: obj
    }

    return $http(req);
}
return factoryObj;
}
]);


我正在按如下方式调用此工厂方法

scope.sendReply = function () {
     updateModel();
     var replyModelValue = scope.replyModel;
     replyMessageFactory.send(scope.replyModel).then(function (data) {
             scope.clear();
             scope.updateThread(replyModelValue);
     });
}


问题是我没有从后端获取最新数据。如果我添加超时,那么它可以工作。也可能是因为后端?

我的后端api是这个

public async Task<HttpResponseMessage> Post(ThreadMessageModel messageData)
{

try
    {
        await _findMessageService.AddMessageReply(findmessage);
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
    catch (Exception exception)
    {
        Log.Log(EventSeverity.Error, new { Message = "Could not create message", exception });
        throw new Exception(string.Format("Kan ikke opprette melding. {0}", exception));
    }
}


public Task<int> AddMessageReply(FindMessage message)
{
try
    {
        client.Update<FindMessage>(message.Id).Field(x => x.Replies, message.Replies).Execute();
         return Task.FromResult(1);
    }
    catch (Exception exception)
    {
        log.Log(EventSeverity.Error, "Could not add message and feedback status", exception);
        throw;
    }
}


有任何想法吗?

最佳答案

您不应该将该scope.updateThread传递给工厂,而应该只返回$ http promise。

commonApp.factory('replyMessageFactory', ['$q', '$http', '$log', function ($q, $http, $log) {

    var factoryObj = {};
    factoryObj.send = function (obj) {

        var req = {
            method: 'POST',
            url: "/api/ThreadMessage",
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            data: obj
        };
        return $http(req);
    }
    return factoryObj;
  }
]);


您会这​​样称呼它:

scope.sendReply = function () {
    replyMessageFactory.send(scope.replyModel)
        .then(function (result) {
            scope.updateThread(result);
        });
}


现在,您的scope.updateThread可以同步工作。



附言关心scope.updateThread参数中的错字,应该是:

scope.updateThread = function (obj) {
    getMessages.getMessageById(obj, scope.updateThreadCallback);
}

07-26 05:49