角度形式:通过按钮选择值

时间:2018-07-18 20:19:30

标签: angular

我有一个角形,我希望用户单击按钮时可以选择一个值。怎么做 ?

page.html

<div *ngFor="let product of products; index as i">
    <button (click)="chooseProduct(i)">{{product.name}}</button>
</div>

page.ts

form: FormGroup;
booking: Booking;
products: Product[];

constructor(private formBuilder: FormBuilder,
            private params: NavParams,) {

  this.form = formBuilder.group({
        [...]
    product: [params.get('item') ? this.booking.product : '',Validators.required],
        [...]
  });

}

chooseProduct(i) {
    ???
}

1 个答案:

答案 0 :(得分:1)

只需传递product,以便每次单击按钮时,您都会获得与该按钮相关的“产品”对象

<div *ngFor="let product of products;">
    <button (click)="chooseProduct(product)">{{product.name}}</button>
</div>