Arrays 选择多个输入并存储到角度为5的数组中
我使用angular 5,我想从下拉列表中选择多个输入,并将所选输入存储到一个数组中Arrays 选择多个输入并存储到角度为5的数组中,arrays,angular,Arrays,Angular,我使用angular 5,我想从下拉列表中选择多个输入,并将所选输入存储到一个数组中 <select multiple class="form-control " name="fea" ngModel size="3" > <option *ngFor="let feature of features | async" [ngValue]="feature"> {{ feature.name }} </option> </sele
<select multiple class="form-control " name="fea" ngModel size="3" >
<option *ngFor="let feature of features | async" [ngValue]="feature">
{{ feature.name }}
</option>
</select>
{{feature.name}
在这里,我正确地选择了多个输入,但如何在数组中存储选定的值
任何帮助都将不胜感激 您可以将选项标记更改为以下
<option *ngFor="let feature of features | async" [ngValue]="feature" (click)="AddtoArray(feature.name)">
{{ feature.name }}
</option>
您可以简单地使用
ngModel
将您的选择添加到您的模型/阵列中
请使用以下内容:-
<select multiple class="form-control " name="fea" ngModel size="3" [(ngModel)]="selectedFeatures">
<option *ngFor="let feature of features" [ngValue]="feature">
{{ feature.name }}
</option>
</select>
{{feature.name}
要获得有效的解决方案,请查看以下stackblitz:-
不要忘记按shift键进行多选
PS:我没有在代码中使用
async
管道,但它也可以正常工作。这可以通过使用(更改)事件轻松实现,如下所示:
<select multiple class="form-control " name="fea" [(ngModel)]="model" (change)="valueAdded(model)">
<option *ngFor="let feature of features | async" [ngValue]="feature">
{{ feature.name }}
</option>
</select>
您想要哪种输入类型?
<select multiple class="form-control " name="fea" [(ngModel)]="model" (change)="valueAdded(model)">
<option *ngFor="let feature of features | async" [ngValue]="feature">
{{ feature.name }}
</option>
</select>
var **yourArray**:string[]; // define array
valueAdded(val){
**yourArray**.Push(val);
}
This will add the selected options from dropDown in array named as **yourArray**