Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 选择多个输入并存储到角度为5的数组中_Arrays_Angular - Fatal编程技术网

Arrays 选择多个输入并存储到角度为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

我使用angular 5,我想从下拉列表中选择多个输入,并将所选输入存储到一个数组中

<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**