Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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可以';t读取$http导致嵌套的$http调用_Angularjs - Fatal编程技术网

AngularJS可以';t读取$http导致嵌套的$http调用

AngularJS可以';t读取$http导致嵌套的$http调用,angularjs,Angularjs,我有以下对两个API的嵌套$http调用,我面临的问题是,即使外部$http调用结果被分配给单独的变量,我也无法从内部$http调用访问外部$http调用结果。有人能告诉我这里缺少什么,以及如何修复它吗?谢谢 clientSvc.getInvoices(clientID).then( function(clientInvoices) { var invoiceID = ''; for (var i=0; i < clientInvoices.Res

我有以下对两个API的嵌套$http调用,我面临的问题是,即使外部$http调用结果被分配给单独的变量,我也无法从内部$http调用访问外部$http调用结果。有人能告诉我这里缺少什么,以及如何修复它吗?谢谢

clientSvc.getInvoices(clientID).then(
    function(clientInvoices) {
        var invoiceID = '';

        for (var i=0; i < clientInvoices.Result.length; i++) {
            invoicesPromise.push(clientSvc.getAR(clientInvoices.Result[i].id).then(
                function(ARList) {

                           //This will always return 3
                           console.log(i);

                    //following line raises an error id of undefined...
                    invoiceID = clientInvoices.Result[i].id; 
                },
                function(status){
                    console.log(status);
                }
            ));
        }

        $q.all(invoicesPromise).then(function() {
            ....
        });
    },
    function(status){
        console.log(status);
    }
);
clientSvc.getInvoices(clientID)。然后(
功能(客户端文件){
var invoiceID='';
对于(变量i=0;i
从闭包引用
i
,但其值在循环中发生变化;当调用
then
success函数时,
i
clientInvoices.Result.length+1
,这就是
clientInvoices.Result[i]
未定义的原因。使用单独的功能,例如:

        for (var i=0; i < clientInvoices.Result.length; i++) {
            invoicesPromise.push(callNested(clientInvoices.Result[i].id));
        }

        function callNested(resultId) {
            return clientSvc.getAR(resultId).then(
                function(ARList) {
                    invoiceID = resultId; 
                },
                function(status){
                    console.log(status);
                }
            )
        }
for(变量i=0;i

尽管如此,您还是多次为单值变量
invoiceID
赋值;这会引起问题。此外,没有
var invoicesPromise=[]在您的代码中。

感谢您的帮助,我已经在代码中添加了var invoicesPromise=[]我刚刚不得不删除它,因为我想只发布导致问题的代码。我已经用控制台更新了问题。log(I)这很奇怪,总是输出3…你知道为什么会这样吗?请注意发票使用$q.all可能这就是所有这些的原因?