首页 > 解决方案 > 如何将第一个组件 id 传递给 Angular2 中的第二个组件?

问题描述

我的问题是我有两个模型,一个是 product 和 taxproduct。在这里我提到了一对多的关系,我已将产品 ID 映射到 taxproduct。在这个 component.ts 中 Save_user 是一个产品服务。Save_tax 是一种税收产品。首先,我想插入产品,并且我想将该 ID 与 TAxproduct 映射。

组件.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);
            });
        }
  }

模型.py

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)

标签: angularangular2-formsangular2-servicesangular2-directives

解决方案


如果我了解您的问题的某些部分,您可以在第一个服务的订阅方法中调用第二个服务。如果您希望从 http 响应返回的内容创建您的组件,您需要等待,然后才能创建您的组件以返回 http 响应。


推荐阅读