2017-07-27 107 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.