2016-08-25 52 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 –