【发布时间】:2020-10-20 09:32:38
【问题描述】:
我有一个产品列表,点击一个产品后,打开产品详细信息,其中有该产品的详细信息。 URL 路径是产品的 id,即在数据库中获取并搜索该产品。但要查看该产品的数据,我必须单击按钮。如何自动执行,然后监听变化?
product-details.component.ts:
export class ProductDetalisComponent implements OnInit {
private ngUnsubscribe = new Subject();
product = [];
product_id;
constructor(private productService: ProductService, private route: ActivatedRoute) { }
ngOnInit() {
this.route.paramMap.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(params =>
{
this.product_id = [params.get('productId')];
this.getProductById();
});
}
getProductById() {
this.productService.getProductById(this.product_id)
.subscribe(
res => {
this.product = res;
},
err => console.log(err)
);
}
ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
}
product-details.component.html:
<button type="button" (click)="getProductById();">Product</button>
<p>Name: <a> {{product.name}} </a></p>
<p>Price: <a> {{product.price}} </a></p>
<p>Id: <a> {{product._id}} </a></p>
【问题讨论】:
标签: angular typescript frontend listener product