angular - 如何将第一个组件 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)
解决方案
如果我了解您的问题的某些部分,您可以在第一个服务的订阅方法中调用第二个服务。如果您希望从 http 响应返回的内容创建您的组件,您需要等待,然后才能创建您的组件以返回 http 响应。
推荐阅读
- javascript - 将点符号字符串转换为对象并查找引用对象
- android - 如何将附近的产品获取到 recyclerView 中的当前用户位置
- java - Gradle:使集成测试源模块工作
- html - 附加对所有人s 与 x 类,除了
- c# - 如何从 ASP.NET Razor 页面中的 .cshtml 页面访问 AspNetUsers 中的数据?
- c++ - 我应该如何正确地将 packaged_task 移动到 lambda?
- dart - 如何从 Stream 中选择 max 或等待下一个(如果它是空的)?
- angular - 在标签上执行特定操作后,标签栏上的 .ionic 4/5 更新计数
- django - Google Cloud App Engine - 使用 PostGreSQL 的 Django App 失去 SQL 连接
- paypal-sandbox - 交易搜索在 PayPalAPI 中返回 AUTHENTICATION_FAILURE