简体   繁体   English

调用$ http以响应另一个http-angular js

[英]call $http in response of another http - angular js

I am new on angular JS. 我是角度JS的新手。 I am tried to implement $http in the response of $http. 我试图在$ http的响应中实现$ http。

My issue is that when i call $http in response of anoter $http. 我的问题是,当我调用$ http作为注释$ http的响应时。 It doesn't show the data on view. 它不会在视图上显示数据。 The view is rendered before the second $http call. 该视图在第二个$ http调用之前呈现。 I have used promises but no luck. 我信守诺言,但没有运气。 Here is the code from other answer which i used by changing a little. 这是我通过稍微改变使用的其他答案的代码。

angular.module('App', [])

.controller('Ctrl', function($scope, resultsFactory) {
  resultsFactory.all().then(
    function(res){
      $scope.results = res;
    },
    function(err){
      console.error(err);
    }
  );
})

.factory('resultsFactory', function($http, $timeout, $q) { 
  var results = {};  

  function _all(){
    var d = $q.defer();
     $http({
       url: url,
       method: 'POST'
     }).then(function (response) {
        var f = {};
        f.id = response.data.id;
        f.name = response.data.name;
        $http({
           url: url,
           data: "id="+response.data.parent_id,
           method: 'POST'
        }).then(function (response1) {
               f.parentname = response1.name;
               d.resolve(f);
        });
     });
    return d.promise;       
  }

  results.all = _all;
  return results;
});

The id and name is shown properly on view but it is showing nothing for parent name. id和名称在视图上正确显示,但父名称没有显示任何内容。 I have debug it. 我已经调试了。 It is undefined when rendering view. 渲染视图时未定义。 After rendering it is setting their value to parentname. 渲染后,将其值设置为parentname。 Can any one help me to resolve this issue? 谁能帮助我解决此问题?

You shouldn't need a deferred for this: just chain the promises: 您不需要为此延期:只需兑现承诺:

 return $http({
   url: url,
   method: 'POST'
 }).then(function (response) {
    var data = {};
    data.id = response.data.id;
    data.name = response.data.name;
    return $http({
       url: url,
       data: "id="+response.data.parent_id,
       method: 'POST'
    }).then(function (response1) {
           data.parentname = response1.name;
           return data;
    });
 });

You overwrote your d variable... 您重写了d变量...

.factory('resultsFactory', function ($http, $timeout, $q) {
    var results = {};

    function _all() {
        var d = $q.defer();
        $http({
            url : url,
            method : 'POST'
        }).then(function (response) {
            var secondD = {};
            secondD.id = response.data.id;
            secondD.name = response.data.name;
            $http({
                url : url,
                data : "id=" + response.data.parent_id,
                method : 'POST'
            }).then(function (response1) {
                secondD.parentname = response1.name;
                secondD.resolve(d);
            });
        });
        return d.promise;
    }

    results.all = _all;
    return results;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM