如何在Angular 7中同时执行多个API请求

时间:2020-01-10 09:26:34

标签: angular angular-httpclient

我在从Angular 7和Typescript的不同页面发送多个请求时遇到问题。

示例:

我有一个名为Email的页面,从该页面我需要一次发送1000封电子邮件,并在变量中更新状态,这应在后台完成(一旦我单击Send,然后应关闭Email表单),然后我已导航到另一个页面来做我的其他事情(将数据提取或发布到另一个API),在这种情况下,直到完成上一个(电子邮件)请求之前,我无法调用第二个API请求。

下面是我发送给APi的代码

   public SendMultileEmails(Emaildata: any, Listcount: number) {

    if (Emaildata != undefined && Emaildata != [] && Emaildata != '') {
        if (Listcount >= this.Emailcount) {
            var Invoice = Emaildata[this.Emailcount];
            var transactionName = Invoice.transactionName;
            var salesHeaderID = Invoice.salesHeaderID;
            var customerurl = '/Api/MultipleEmail?trnasactiontype=' + transactionName + '&Headerid=' + salesHeaderID;
            this.http.get(customerurl)
                .subscribe(result => {
                    this.Emailcount = this.Emailcount + 1;
                    this.SentEmailcount = this.Emailcount;
                    this.SendMultileEmails(Emaildata,Listcount);
                },
                    err => console.log(err)
                );
        }


    }


}

有没有一种方法可以执行此任务。请引导我。

谢谢

维克多。

1 个答案:

答案 0 :(得分:1)

我认为您想做些类似的事情:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Random Word Generator</title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript"></script>
    <script language="javascript" src="update.js" type="text/javascript"></script>
    <script language="javascript" src="words.js" type="text/javascript"></script>
</head>
<body onload="updateChat();">

<p id=shownword></p>

    <form action="" method="post">
    <input type="submit" name="word"
                class="button" value="a" id="a"/> 
          
        <input type="submit" name="word"
                class="button" value="b" id="b"/> 
    </form>
    
    <?php
    //This function gets called when button is pushed
    function postword(){
        $fp = fopen('word.txt', 'w');
        fwrite($fp, $_POST['word']);
        fclose($fp);
        

        echo '<script type="text/javascript">',
        'firstLetterSearch(';
        echo $_POST['word'];
        echo ');',
        '</script>';
        
    }
    //When the button is pushed, the function will be called
    if (isset($_POST['word'])) {
        postword();
        //return;
    }
    ?>

</body>
</html>