我应该如何从Angular-2中的下拉列表中使用多选选项绑定数组

问题描述 投票:1回答:1

我正在尝试使用多选下拉列表绑定数组对象,因此我可以将选定的值传递给模型以与其他模型详细信息一起保存。

这里我使用change事件来接收所选的选项值,但函数不起作用,这是我的代码:

HTML代码:

label class="col-md-3 form-control-label text-right">Customer Name</label>
           <div class="col-md-7">
             <select  multiple="multiple" class="js-example-basic-multiple form-control selectsizing" (change)="oncustomerSelect($event.target.value)" name="customer_name" [(ngModel)]="customer_name.customer" #customer_name="ngModel" [ngClass]="{ 'is-invalid': f.submitted && customer_name.invalid }"  required>
              <option></option>
              <option [value]="customernames.Business_name" *ngFor="let customernames of model_customername">{{customernames.Business_name}}</option>
     </select>

            </div>

在选择下拉列表的任何值时,应该调用函数oncustomerSelect(),这在component.ts中有描述,但是这个函数调用不能完成。

这是我的Component.ts:

 customer_name:any={
    customer:[]
};    
model:any={
product_type:"in stock",    
item_code:"ASD34", 
customer_name:[]=[]
} 

oncustomerSelect(value){
console.log("customer select::"+JSON.stringify(value));
var i=0;
this.model.customer_name[i]=value; 
i++; 
console.log("model customer::"+JSON.stringify(this.model.customer_name));  
}

请帮帮我,如何在多个select中调用该函数,如何将数组与所选值绑定?

谢谢!

javascript angularjs angular2-template angular2-forms
1个回答
0
投票

检查参考!在给定的链接中,您可以看到您将获得下拉选择的值,您可以添加多选属性并完成此操作

angular5-select-option-dropdown

© www.soinside.com 2019 - 2024. All rights reserved.