admin管理员组文章数量:1415698
I'm trying to populate an array with all selected values from a mat-select dropdown. The goal is to create an input field for every selected value like this:
Image
I thought about calling a method every time I select a value, but I'm not really sure on what to do next... This is what I have:
MyComponentponent.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>
MyComponentponent.ts
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'm-myponent',
templateUrl: './myponentponent.html',
styleUrls: ['./myponentponent.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);
}
}
}
How can I call a method in the html file and populate the productsToReturn array?
Thanks!
I'm trying to populate an array with all selected values from a mat-select dropdown. The goal is to create an input field for every selected value like this:
Image
I thought about calling a method every time I select a value, but I'm not really sure on what to do next... This is what I have:
MyComponent.ponent.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.ponent.ts
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'm-myponent',
templateUrl: './myponent.ponent.html',
styleUrls: ['./myponent.ponent.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);
}
}
}
How can I call a method in the html file and populate the productsToReturn array?
Thanks!
Share Improve this question edited Mar 1, 2019 at 14:27 H. Soares asked Mar 1, 2019 at 12:33 H. SoaresH. Soares 2036 silver badges22 bronze badges3 Answers
Reset to default 2You didn't specify when you want to access the selected objects.
Its simple if you want to access it when the form is submitted, like on a click of a button. Then you can use this.products.value
to get the selected options.
If you want it at the time of selection, then you can bind to selectionChange()
event
<mat-select placeholder="Products" [formControl]="products" multiple (selectionChange)="onSelectionChange($event) >
then in ts file you can get the selected options
onSelectionChange(e){
console.log(e.value); //or
console.log(this.toppings.value);
}
You don't need to do this manually. Just bind the value
property of the mat-option
to the current product
string and your FormControl
will contain the array of selected options.
Template
<mat-form-field>
<mat-select placeholder="Products" [formControl]="products" multiple>
<mat-option *ngFor="let product of productsList" [value]="product">{{ product }}</mat-option>
</mat-select>
</mat-form-field>
And in your ponent you can access the selected options via this.products.value
.
Here is a minimal stackblitz with a working solution. The selected values are directly displayed next to the
mat-select
.
I solved my problem... I made the following changes based on j4rey's answer
MyComponent.ponent.html
<mat-form-field>
<mat-select placeholder="Products" [formControl]="products" multiple>
<mat-option *ngFor="let product of productsList" [value]="product" (onSelectionChange)="onSelectionChange(product)">{{ product }}</mat-option>
</mat-select>
</mat-form-field>
<div class="row" *ngFor="let product of productsToReturn">
<div class="col-4">
<mat-form-field>
<input matInput disabled="disabled" [value]="product">
</mat-form-field>
</div>
<div class="col-2">
<mat-form-field class="prod-qty">
<input matInput step="1" value="1" min="1" type="number">
</mat-form-field>
</div>
</div>
MyComponent.ponent.ts
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'm-myponent',
templateUrl: './myponent.ponent.html',
styleUrls: ['./myponent.ponent.scss']
})
export class MyComponent implements OnInit {
products = new FormControl();
productsList = ['Prod1', 'Prod2', 'Prod3', 'Prod4', 'Prod5', 'Prod6'];
productsToReturn = [];
constructor() { }
ngOnInit() { }
onSelectionChange(product: string) {
if (!this.productsToReturn.includes(product)) {
this.productsToReturn.push(product);
} else {
let index = this.productsToReturn.indexOf(product);
this.productsToReturn.splice(index, 1);
}
}
}
本文标签: javascriptHow can I populate an array with selected values from dropdown in AngularStack Overflow
版权声明:本文标题:javascript - How can I populate an array with selected values from dropdown in Angular? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745238534a2649182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论