My task is to dynamically populate an array with all selected values from a mat-select dropdown in Angular. The end goal is to generate an input field for each selected value, similar to the example shown below:
I've been contemplating calling a method every time a value is selected, but I'm uncertain about the next steps... Here's what I have so far:
MyComponent.component.html
<mat-form-field>
<mat-select placeholder="Products" [formControl]="products" multiple>
<mat-option *ngFor="let product of productsList">{{ product }}</mat-option>
</mat-select>
</mat-form-field>
MyComponent.component.ts
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'm-mycomponent',
templateUrl: './mycomponent.component.html',
styleUrls: ['./mycomponent.component.scss']
})
export class MyComponent implements OnInit {
products = new FormControl();
productsList = ['Prod1', 'Prod2', 'Prod3', 'Prod4', 'Prod5', 'Prod6'];
productsToReturn = [];
constructor() { }
ngOnInit() {
}
fillProductsToReturn(product){
if(!this.productsToReturn.includes(product)){
this.productsToReturn.push(product);
}
}
}
I'm looking for guidance on how to trigger a method within the HTML file to update the productsToReturn array. Any help would be greatly appreciated!
Thank you!