我有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 –