使用.then不会被解雇的服务方法

时间:2017-06-14 10:42:08

标签: javascript angularjs

下面是我的AngularJs代码,我试图在succussfull ProcessCriteria调用之后调用TalentPoolService.search(),但由于某种原因它没有访问TalentPool.Service。我在这里做错了什么?

       $scope.Search = function (item, SolrLabel) {
    if (item != null) {
        console.log('Item: ' + item.Key);
        console.log('SolrLabel: ' + SolrLabel);
        console.log($localStorage.message);

        var pvarrData = new Array();
        pvarrData[0] = JSON.stringify($localStorage.message);
        pvarrData[1] = item.Key;
        pvarrData[2] = SolrLabel;

            $http({
                method: 'POST',
                url: '/api/TalentPool/ProcessCriteria',
                data: JSON.stringify(pvarrData),
                headers: { 'Content-Type': 'application/json' }
            }).then(function (response) {
                console.log('ProcessCriteria Success fired');
                $localStorage.message = response.data;
                console.log(response.data);
                return response.data;
                },
                    function (response) { 
                        // failed
                        console.log('facet post error occured!');
                    }).then(

                          function () {
                              TalentPoolService.search().then(function successCallback(response1) {
                                  $scope.talentpoolist = response1.data.model;
                                  $localStorage.message = response1.data.baseCriteria;
                                  console.log('TalentPoolService.search successCallback fired');
                                  setTimeout(function () {
                                      LetterAvatar.transform();
                                  }, 20);
                              }, function errorCallback(response1) {
                                  $scope.errors = [];
                                  $scope.message = 'Unexpected Error while saving data!!' + response;
                              })
                          }
                    );
    }
}

1 个答案:

答案 0 :(得分:1)

您必须返回链接数据才能正常工作。

$http({
  method: 'POST',
  url: '/api/TalentPool/ProcessCriteria',
  data: JSON.stringify(pvarrData),
  headers: {
    'Content-Type': 'application/json'
  }
}).then(function(response) {
    console.log('ProcessCriteria Success fired');
    $localStorage.message = response.data;
    console.log(response.data);
    return response.data; // **return here**
  },
  function(response) {
    // failed
    console.log('facet post error occured!');
  }).then(

  function() {
    TalentPoolService.search().then(function successCallback(response1) {
      $scope.talentpoolist = response1.data.model;
      $localStorage.message = response1.data.baseCriteria;
      setTimeout(function() {
        LetterAvatar.transform();
      }, 20);
    }, function errorCallback(response1) {
      $scope.errors = [];
      $scope.message = 'Unexpected Error while saving data!!' + response;
    })
  }
);

为什么因为,您正在使用的下一个版本需要处理一些数据。所以,如果你不回来就不能。所以,必须返回数据。