2016-08-25 75 views
2

我有2個下拉列表,並根據下拉列表1的值填充第二個下拉列表。我有編輯方案並根據網格填充下拉值。角2等待同步方法

enter image description here

private populateContacts(relationship: Relationship) { 
     this.getContacts(relationship.businessAreaId); 
     this.selectedContact = this.contacts.find(contact => contact.id === relationship.contacts[0].id); 
    } 

    private getContacts(businessAreaObjorId) { 
     if (businessAreaObjorId === NaN) 
      businessAreaObjorId = businessAreaObjorId.id; 
     this.businessAreaService.getContacts(businessAreaObjorId) 
      .subscribe(
      contacts => this.contacts = contacts, 
      error => this.errorMessage = error); 
    } 

HTML是下面

<tr *ngFor="let relationship of relationships"> 
       <td>{{relationship.supplierName}}</td> 
       <td>{{relationship.businessArea}}</td> 
       <td>{{relationship.contacts[0].name}}</td> 
       <td><a href="javascript:void(0)" (click)="onEdit(relationship)">Edit</a></td> 
      </tr> 
      <tr class="active"> 
       <td> 
        <select class="form-control" name="supplier" required 
          [(ngModel)]="selectedSupplier" #supplier="ngModel"> 
         <option *ngFor="let supplier of suppliers" [ngValue]="supplier">{{supplier.name}}</option> 
        </select> 
        <div [hidden]="supplier.valid || supplier.pristine" class="alert alert-danger"> 
         Supplier is required 
        </div> 
       </td> 
       <td> 
        <select class="form-control" name="businessArea" required 
          [(ngModel)]="selectedBusinessArea" #businessArea="ngModel" (ngModelChange)="getContacts($event)"> 
         <option *ngFor="let businessArea of businessAreas" [ngValue]="businessArea">{{businessArea.name}}</option> 
        </select> 
        <div [hidden]="businessArea.valid || businessArea.pristine" class="alert alert-danger"> 
         Business Area is required 
        </div> 
       </td> 
       <td> 
        <select class="form-control" name="contact" required 
          [(ngModel)]="selectedContact" #contact="ngModel"> 
         <option *ngFor="let contact of contacts" [ngValue]="contact">{{contact.name}}</option> 
        </select> 
        <div [hidden]="contact.valid || contact.pristine" class="alert alert-danger"> 
         Contact is required 
        </div> 
       </td> 
       <td> 
        <input type="button" value="Add" class="btn btn-default" (click)="onSubmit()" [disabled]="!factoryRelationshipForm.form.valid" /> 
       </td> 
      </tr> 

當我點擊編輯鏈接onEdit方法被調用和選擇/分配值,但失敗上的觸點的selction。

private onEdit(relationship: Relationship): void { 
    relationship.inEditMode = true; 
    this.selectedSupplier = this.suppliers.find(supplier => supplier.id === relationship.supplierId); 
    this.selectedBusinessArea = this.businessAreas 
     .find(businessArea => businessArea.id === relationship.businessAreaId); 
    this.populateContacts(relationship); 
} 

private populateContacts(relationship: Relationship) { 
    this.getContacts(relationship.businessAreaId); 
    this.selectedContact = this.contacts.find(contact => contact.id === relationship.contacts[0].id); 
} 

當我試圖找到聯繫人數組中的聯繫人時,它失敗。

enter image description here

我認爲原因是service.getcontacts是HTTP調用,它需要一定的時間,當我試圖找到this.contacts陣列是不確定在接觸對象。

有沒有辦法在populateContacts方法中等待getContacts?

編輯1:

我新的代碼看起來像這樣

private populateContacts(relationship: Relationship) { 
     //TODO: find a way to use this.contacts/getContacts 
     this.businessAreaService.getContacts(relationship.businessAreaId) 
      .subscribe(val => { 
       console.log(val); 
       this.contacts = val; 
       this.selectedContact = this.contacts.find(contact => contact.id === relationship.contacts[0].id) 
      }); 
    } 

private getContacts(businessAreaObj) { 
    this.businessAreaService.getContacts(businessAreaObj.id) 
     .subscribe(
     contacts => this.contacts = contacts, 
     error => this.errorMessage = error); 
} 

編輯2:

@Gunter」解決方案適用於編輯的情況下,但如果我手動選擇下拉值在BA上它不會加載聯繫人下拉列表中的聯繫人。

enter image description here

+0

能否請你告訴你的編輯按鈕的HTML代碼? –

+0

請檢查更新html –

回答

1

您需要返回Observable。對於這一點,你不能調用subscribe()

private getContacts(businessAreaObjorId) { 
    if (businessAreaObjorId === NaN) 
     businessAreaObjorId = businessAreaObjorId.id; 
    return this.businessAreaService.getContacts(businessAreaObjorId) 
     .catch(error => this.errorMessage = error) 
     .map(
     contacts => this.contacts = contacts); 

    } 
} 

那麼你可以調用它,得到一個Observable您可以訂閱,並在回調調用跟進代碼:

private populateContacts(relationship: Relationship) { 
    this.getContacts(relationship.businessAreaId) 
    .subscribe(val => this.selectedContact = this.contacts.find(contact => contact.id === relationship.contacts[0].id)); 
} 
+0

的問題感謝您的回覆Gunter,this.businessAreaService.getContacts已經是可觀察的。它的一個http調用。我在這裏有2個場景。 1.根據BA值填充聯繫人下拉列表2.單擊編輯時選擇值。我不需要觀察第一種情況。它就像我在這兩種情況下調用相同的方法。但讓我先試試你的解決方案:) –

+0

如上面的預期解決方案完美地適用於編輯案例,但是當我創建新記錄並從BA中選擇值時,它不填充聯繫人下拉列表,因爲我沒有訂閱更改...我認爲getContacts應該訂閱和填充聯繫人我會直接調用服務?我試圖避免另一個http調用並使用相同的數組this.contacts。有沒有辦法? –

+0

按照所有這些細節有點複雜。聽起來像http://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-http-network-call-in –