【发布时间】:2016-08-25 09:45:29
【问题描述】:
我有 2 个下拉列表,我根据下拉列表 1 的值填充第二个下拉列表。我有场景可以根据网格编辑和填充下拉值。
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 方法被调用并选择/分配值,但在选择联系人时失败。
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 数组中找到 undefined 的联系人对象时。
有没有办法在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的问题
标签: angular typescript promise