Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 与jQuery$.get()一样,angular$q.all()是否还有第二次成功_Javascript_Ajax_Angularjs_Http - Fatal编程技术网

Javascript 与jQuery$.get()一样,angular$q.all()是否还有第二次成功

Javascript 与jQuery$.get()一样,angular$q.all()是否还有第二次成功,javascript,ajax,angularjs,http,Javascript,Ajax,Angularjs,Http,查看jQuery文档,我发现了以下内容: $.get( "example.php", function() { alert( "success" ); }) .done(function() { alert( "second success" ); <--- }) .fail(function() { alert( "error" ); }) .always(function() { alert( "finished" ); });

查看jQuery文档,我发现了以下内容:

$.get( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" ); <---
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });
 $q.all([
            $http.get(ROOT + "Lookup/GetStates"),
            $http.get(ROOT + "Lookup/GetCountries"),
            $http.get(ROOT + "Address/GetAddresses"),
        ]).then(function (results) {
            $scope.states = jQuery.parseJSON(results[0].data.data);
            $scope.country = jQuery.parseJSON(results[1].data.data);
            $scope.addresses = jQuery.parseJSON(results[3].data);
        });
$http.get('parent request')
.then(function(win){
      // do something when everything is resolved
   },
   function(err){
      //handle error
   },
   function(progress){
      //do something when a child request is resolved
   }
);
在执行
then
之后(仅在then之后)我需要调用一个方法
$scope.setupControls()


这有可能吗?

为什么你不能这样做:

   $q.all([
        $http.get(ROOT + "Lookup/GetStates"),
        $http.get(ROOT + "Lookup/GetCountries"),
        $http.get(ROOT + "Address/GetAddresses"),
    ]).then(function (results) {
        $scope.states = jQuery.parseJSON(results[0].data.data);
        $scope.country = jQuery.parseJSON(results[1].data.data);
        $scope.addresses = jQuery.parseJSON(results[2].data);
        $scope.setupControls();
    });
我认为没有必要再成功一次。保持简单(性感?

看看这个:

.finally(function() {
  // do this on both error and success
});
正如它所说,在成功和错误之后执行

因此,完整代码:

$q.all([
        $http.get(ROOT + "Lookup/GetStates"),
        $http.get(ROOT + "Lookup/GetCountries"),
        $http.get(ROOT + "Address/GetAddresses"),
    ]).then(function (results) {
        $scope.states = jQuery.parseJSON(results[0].data.data);
        $scope.country = jQuery.parseJSON(results[1].data.data);
        $scope.addresses = jQuery.parseJSON(results[3].data);
    }).finally(function() { /* <-- here */
      // do this on both error and success
    });
$q.all([
$http.get(ROOT+“Lookup/GetStates”),
$http.get(ROOT+“Lookup/GetCountries”),
$http.get(ROOT+“地址/GetAddresses”),
]).然后(函数(结果){
$scope.states=jQuery.parseJSON(结果[0].data.data);
$scope.country=jQuery.parseJSON(结果[1].data.data);
$scope.addresses=jQuery.parseJSON(结果[3].data);

}).finally(function(){/*您应该能够多次调用

链接承诺 因为调用承诺的then方法会返回一个新的派生承诺,所以很容易创建承诺链:


Angular的$q提供了与jQuery相同的方法

我可以想到的一种方法是:

$.get( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" ); <---
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
  });
 $q.all([
            $http.get(ROOT + "Lookup/GetStates"),
            $http.get(ROOT + "Lookup/GetCountries"),
            $http.get(ROOT + "Address/GetAddresses"),
        ]).then(function (results) {
            $scope.states = jQuery.parseJSON(results[0].data.data);
            $scope.country = jQuery.parseJSON(results[1].data.data);
            $scope.addresses = jQuery.parseJSON(results[3].data);
        });
$http.get('parent request')
.then(function(win){
      // do something when everything is resolved
   },
   function(err){
      //handle error
   },
   function(progress){
      //do something when a child request is resolved
   }
);

…当“父请求”发出一些子请求时,对于每个已解析的子请求,您可以使用
.notfiy()
方法更新父请求,并且您应该跟踪所有子请求状态,当所有请求都已解析时,您可以调用
.resolve()
并执行任何您想要的操作(
$scope.setupControls()
在您的情况下)。

您为什么要使用
parseJSON()
?@charlietfl坦白说,我不确定。我正在发送一个JsonResult。这不是必需的?不,如果发送正确,就不应该这样