簡體   English   中英

如何將第一個組件ID傳遞給Angular2中的第二個組件?

[英]How to pass first component id to second component in Angular2?

我的問題是我有兩種模式,一種是產品和稅收產品。 在這里,我提到了一對多的關系,我已將產品ID映射到taxproduct。 在此component.ts中,Save_user是產品服務。 而Save_tax是一種稅收產品。 首先,我想插入產品,我想使用該ID與TAxproduct進行映射。

component.ts

 createNewProduct(productForm: NgForm) {
    this.productService.save_user(productForm.value)
      .subscribe(response => {
        const toast_parameter = this.notificationService.getToast('success', 'New Product', 'Inserted Successfully');
        this.toastConfig = toast_parameter.config;
        this.toasterService.popAsync(toast_parameter.toast);
      },
        error => {
          const toast_parameter = this.notificationService.getToast('error', 'New Product', 'Error Occurred!!');
          this.toastConfig = toast_parameter.config;
          this.toasterService.popAsync(toast_parameter.toast);
        });
 for (let i = 0; i < this.contacts.length; i++) {
          console.log(this.contacts[i])
          this.productService.save_tax(JSON.stringify(this.contacts[i] )).subscribe(response => {
            const toast_parameter = this.notificationService.getToast('success', 'New Product', 'Inserted Successfully');
            this.toastConfig = toast_parameter.config;
            this.toasterService.popAsync(toast_parameter.toast);
          },
            error => {
              const toast_parameter = this.notificationService.getToast('error', 'New Product', 'Error Occurred!!');
              this.toastConfig = toast_parameter.config;
              this.toasterService.popAsync(toast_parameter.toast);
            });
        }
  }

模型

class Product(models.Model):
    image = models.ImageField(upload_to='myphoto/%Y/%m/%d/', null=True, max_length=255)
    pro_name =  models.CharField(max_length=25)
    description = models.CharField(max_length=150)
    category = models.ForeignKey(Category,on_delete=models.CASCADE)
    sales = models.CharField(max_length=25)
    cost = models.CharField(max_length=25)
    taxable = models.BooleanField(default=False, blank=True)
    tax_details= models.CharField(max_length=250)
    type_units = models.ForeignKey(Units, on_delete=models.CASCADE)
    hsn = models.CharField(max_length=10)


class Taxproduct(models.Model):
    tax_name = models.CharField(max_length=50)
    tax_percentage = models.CharField(max_length=3)
    product_id = models.ForeignKey(Product, on_delete=models.CASCADE)

如果我了解您的問題的一部分,則可以在第一個服務的subscription方法內調用第二個服務。 如果您希望從http響應返回的內容中創建組件,則需要等待一段時間才能創建組件以使HTTP響應返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM