首页 > 解决方案 > 发布新数据后,是否可以从 web api 角度获取当前 id 的值?

问题描述

我正在生成一张包含许多销售交易的发票。最初选择状态为假且有许多交易的客户并生成发票。现在,在生成发票时,我希望这些状态为真,并且所有选定的交易都分配给新生成的发票。

我能够根据所选交易生成发票。但现在我无法将该发票 ID 分配给这些交易。

如果我能得到当前生成的发票 ID,那将很容易。谁能帮我?

ngOnInit() {
  this.id = this.route.snapshot.paramMap.get('id');
  this.service.getSalesTransaction(this.id).subscribe((singleData: any) => {

    this.service.getAllSalesTransactions().subscribe((data: any) => {
      data.forEach(element => {
        if (singleData.CustomerName === element.CustomerName 
            && element.Status === false) {
          //Pushing valid object into an array
          this.salesTransaction.push(element);

          //Calculating Sub Total
          this.subTotal = this.salesTransaction.reduce((acc, val) => acc+=val.Total, 0);
          console.log(this.subTotal);

          //Calculating Tax based on the amount
          if (this.subTotal <= 500) {
            this.tax = (10 / 100) * this.subTotal;
            console.log(this.tax);
          } else {
            this.tax = (5 / 100) * this.subTotal;
            console.log(this.tax);
          }
        }
      });

      //Preparing data for invoice database
      this.invoiceData.Tax = Math.round(this.tax);

      //Creating new Invoice
      this.serviceInvoice.createInvoice(this.invoiceData).subscribe((dataInvoice) => {
        console.log('Invoice -', dataInvoice);
        console.log(this.invoiceData);
      });
    });
  });
});
}

标签: angulartypescript

解决方案


这是因为这个服务调用的行为是异步的,有时如果你使用同步的方式,会解决这个问题

角度导向


推荐阅读