Reputation: 1892
I have a angular form and I would like to be able to select a value when my user click on a button. How to do it ?
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) {
???
}
Upvotes: 1
Views: 329
Reputation: 24472
Just pass the product
so every time you click the button you will get the 'product' object related to that button
<div *ngFor="let product of products;">
<button (click)="chooseProduct(product)">{{product.name}}</button>
</div>
Upvotes: 1