Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
AngularJS承诺使用同步Http调用_Angularjs_Http_Synchronous - Fatal编程技术网

AngularJS承诺使用同步Http调用

AngularJS承诺使用同步Http调用,angularjs,http,synchronous,Angularjs,Http,Synchronous,在执行多个同步http调用时,我的一个控制器中没有调用then函数,这是我面临的一个问题 在下面的代码中,我的控制器中的console.logdata语句没有被执行,因为控件从未到达then函数 ********************控制器文件 function MyController($scope, $location, $timeout) { function submit($event) { Myservice.getDetails(productid).then

在执行多个同步http调用时,我的一个控制器中没有调用then函数,这是我面临的一个问题

在下面的代码中,我的控制器中的console.logdata语句没有被执行,因为控件从未到达then函数

********************控制器文件

function MyController($scope, $location, $timeout) {

    function submit($event) {
      Myservice.getDetails(productid).then(function(data){
        console.log(data) 
      });
    } 


angular.module('myModule')
    .controller('MyController', MyController);
})(window.angular);
******************我的服务

(function (angular, mySecondService) {
  'use strict';

  function MyService($http, $q, $log, $location, mySecondService{

    function getDetails(pId) {
      byodCheckerData = [];
      mySecondService.httpTkn().then(function(Tkn){
          var request = {
            method: 'GET',
            url: 'getDetails',
            headers: {auth: Tkn},
            params: {Id:pId}
          };

          return $http(request).then(function(resp){
            return resp;
          })                
      })              
    }; 

    return {
      getDetails: getDetails;
  }

angular.module('myModule')
.factory('MyService', MyService);

})(window.angular);
***********************************MySecondservice

(function (angular) {
  'use strict';
  function mySecondService($log, $http, $q) { 
    var ts = this;
      function httpTkn(){
          defer = $q.defer();
          var authReq = {
            method: 'GET',
            url: 'getTkn'
          };
         return $http(authReq).then(function (response) {
              defer.resolve(response.data.tkn
        return response.data.tkn;
            },
            function (error) {

              return error;
            });
      };


    ts.httpTkn = httpTkn;
  }

  angular.module('myModule')
    .service('mySecondService', mySecondService);

})(angular);.

您可以尝试以下代码:

function getDetails(pId) {
    var defered = $q.defer();
    byodCheckerData = [];
    mySecondService.httpTkn().then(function(Tkn) {
        var request = {
            method: 'GET',
            url: 'getDetails',
            headers: { auth: Tkn },
            params: { Id: pId }
        };

        $http(request).then(function(resp) {
            defered.resolve(resp);
        });
    });
    return defered.promise;
}

请注意,您需要返回promise,以便在解析$http值时该函数可以工作

很抱歉回复太晚。这对我有用。谢谢你,沙尚克。我不确定我是否可以在这里问这个问题,但如果你不介意的话,我们如何为这个场景编写一个karma测试用例呢?同步嵌套http调用。你是指控制器的测试用例,在这个测试用例中你必须创建一个带有getDetails方法的模拟服务???是的,请。我已经编写了测试用例来检查使用模拟服务获取细节函数的行为。如果我们想测试像第二个http调用这样的同步功能,就要等到第一个http调用完成。