将参数添加到ng-click中

时间:2018-08-30 14:25:45

标签: angular

如何将cart.id添加到this.props.history.push('/job' + id')方法中并在该功能中使用它?

(click)="onRemove()"

2 个答案:

答案 0 :(得分:0)

只需将其作为参数传递

(click)="onRemove(cart.id)" 

onRemoveProduct(cartId: string) {

答案 1 :(得分:0)

ngOnInit() {
  this.route.paramMap.pipe(
    tap((params: ParamMap) => this.cartId = params.get('id')),
    switchMap((params: ParamMap) => this.mhttp.deleteFromCart(params.get('id')))
  .subscribe((response: any) => {
    this.product = response;
  });
}

onRemove() {
  if (this.cartId) {
    console.log(this.cartId);
    console.log(this.product);
  } else {
    console.log('observable has not fired yet');
  }
}

然后只需在模板(cartId)或.ts文件(this.cartId)中的任何位置访问它-例如

这是一个合适的点击处理程序... (click)="onRemove()"