Angularjs $ http请求在没有完成它的工作的情况下通过

时间:2018-05-21 13:32:19

标签: angularjs http service

您好我在我的项目中使用angularjs 1.6我遇到了$ http请求的问题,或者我不知道那里发生了什么。我将尝试解释下面的问题;

app.controller('ctname', function($scope, fcname){

  $scope.calledFunc = function(){

    fcname.serviceFunction(12).then(function(response){
      if(response.data != null){
        //fill modal's model here 
      }
    });

    //Open modal
    $('#SomeModal').modal('show');

  };

})
.factory('fcname', function($http){
var fac = {};

fac.serviceFunction= function (someId) {
        return $http({
            url: '/Home/someBackEndMethod',
            data: JSON.stringify({ _someId: someId}),
            method: 'POST',
            headers: { 'content-type': 'application/json' }
        });
    };

return fac;
});

究竟发生了什么,我的模态在服务之前打开了,所以我的模态有时会打开。我能做些什么吗?

1 个答案:

答案 0 :(得分:1)

JavaScript和AngularJS框架使用非阻塞异步I / O. 后续代码总是在事件处理程序提供给那些I / O操作之前执行

需要在I / O操作之后执行的代码事件处理程序中

app.controller('ctname', function($scope, fcname){

  $scope.calledFunc = function(){

    fcname.serviceFunction(12)
      .then(function(response){
        if(response.data != null){
           //fill modal's model here 
        }
    }).catch(function(error) {
        console.log(error);
    }).finally(function() {            
        //Open modal
        $('#SomeModal').modal('show');
    });

  };

})

将代码放在原始.then块或后续chained块中。

相关问题