【问题标题】:How to pass first component id to second component in Angular2?如何将第一个组件 id 传递给 Angular2 中的第二个组件?
【发布时间】:2018-10-17 11:36:07
【问题描述】:

我的问题是我有两个模型,一个是 product 和 taxproduct。在这里我提到了一对多的关系,我已经将产品 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);
            });
        }
  }

Model.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)

【问题讨论】:

    标签: angular angular2-forms angular2-services angular2-directives


    【解决方案1】:

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

    【讨论】:

    • 其实我是 angular2 的新手。所以我很困惑。我无法映射主表单 ID。它会引发类似 {"product_id":["This field is required."]} 的错误。你能告诉我怎么做吗
    • 您的查询仍不清楚。请粘贴更多代码并分部分解释您的问题。
    • 我已经提到了我的模型和component.ts,所以请检查上面的代码。
    • “首先我想插入产品,我想将该 id 与 TAxproduct 映射”-> 我想你做的第一部分是正确的。我不明白你接下来要做什么。什么是“那个id”?在您的函数中,您基本上是通过服务保存传递给函数的参数。这是可以理解的。接下来,您将遍历联系人数组并将每个对象保存在该数组中。你能抛出一些代码来显示联系人在图片中的位置吗?也许一些 HTML 会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 2014-11-05
    • 2020-06-02
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多