2017-07-27 109 views
0

我有两个数组serviceOptionsreactivePackages。针对每个服务选项,都有一系列反应包(显示在下拉列表中)。Angular2下拉选择循环

现在我在循环中动态创建下拉菜单。我的问题是如何获得选择表单提交下拉选项? (这是形式的一部分)

<label *ngFor="let service of serviceOptions"> 
    <b>{{service.serviceName}}</b> 
    <br /><br /> 
    <md-select placeholder="Select Package" formControlName="packageName" size="30"> 
     <ng-container *ngFor="let package of (reactivePackages | async)"> 
     <md-option *ngIf="service.serviceId==package.serviceId" [value]="package" (click)="hello()"> 
      {{ package.packageName }} 
     </md-option> 
     </ng-container> 
    </md-select> 
    <br /> <br /> 
    </label> 
+0

你会怎样想的是,表单对象样子? – Alex

回答

1
You can achieve it by saving the selected option in a variable. 

    You need to follow just two steps. 
    1. Create a method on component.ts that will store the selected option. 
    Example- 

     selectedData(event: any) { 
     this.storedData = event.target.value; 
     } 

    2. Call this method on html. 
    Example- 

<select class="form-control" (change)="selectedData($event)"> 
<option *ngIf="service.serviceId==package.serviceId" [value]="package">{{ package.packageName }}</option> 
</select> 

3.Then simply call a method that will save the selected data.