我有2個下拉列表,並根據下拉列表1的值填充第二個下拉列表。我有編輯方案並根據網格填充下拉值。角2等待同步方法
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);
}
當我試圖找到聯繫人數組中的聯繫人時,它失敗。
我認爲原因是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上它不會加載聯繫人下拉列表中的聯繫人。
能否請你告訴你的編輯按鈕的HTML代碼? –
請檢查更新html –