【发布时间】:2019-10-15 10:13:32
【问题描述】:
我有一个 Angular 8 应用程序和一个下拉列表。下拉列表的内容来自后端。
但如果我从下拉列表中选择一个值,该值将保持未定义或未选择状态。
但是如果我做一个硬编码的值,那么它就可以工作。
这是下拉列表:
filterByQrCodes() {
this.participantService
.filterParticipantsByQRCode(1, this.selectedValue as any, moment(this.startDate).format("YYYY MM D"), this
.selectedValueOptie as any)
.subscribe(filterByQrcode => {
console.log(filterByQrcode);
console.log("selectedValue", this.selectedValue as any);
console.log("selectedValueOption", this.selectedValueOptie);
this.filterParticipantByQrcode.emit(filterByQrcode);
});
this.showDropdownChallenge = true;
}
这是模板:
<div class="search-select searchoptions" *ngIf="showDropdownVcheqCode && selectedSearch" >
<mat-select placeholder="Opties" name="option" [(ngModel)]="selectedValueOptie" (change)="getVcheqCodes()">
<mat-option *ngFor="let option of (returnEcheqCodes$ | async)" value="option.value"> {{ option.title }} </mat-option>
</mat-select>
</div>
但如果我选择一个值,我会收到此错误:
error:
EcheqFamilyId: ["The value 'option.value' is not valid for EcheqFamilyId."]
__proto__: Object
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for https://dev-engine.mijnhep.nl/api/medical/organisation/1/Participant/filter-by-echeq?Filter=New&Start=2019%2010%203&EcheqFamilyId=option.value: 400 Bad Request"
name: "HttpErrorResponse"
ok: false
status: 400
statusText: "Bad Request"
但是例如,如果我这样做:
filterByVchecCode() {
this.participantService
.filterParticipantsByEcheq(1, this.selectedValue as any, moment(this.startDate).format("YYYY MM D"),
"4597544a-f402-4bd0-870e-cc053ddf7cd0")
.subscribe(filterByEcheqCode => {
console.log(filterByEcheqCode);
console.log("SelectedValueOption", this.selectedValueOptie as any);
this.filterParticipatnByVcheqCode.emit(filterByEcheqCode);
});
}
然后我得到正确的数据。
如果我必须提供更多信息。你可以说出来
好吧,如果我这样做:
<div class="search-select searchoptions" *ngIf="showDropdownVcheqCode && selectedSearch" >
<mat-select placeholder="Opties" name="option" [(ngModel)]="selectedValueOptie" (change)="getVcheqCodes()">
<mat-option *ngFor="let option of (returnEcheqCodes$ | async)" value="{{option.value}}"> {{ option.title | json }} </mat-option>
</mat-select>
</div>
然后我会得到这个错误:
EcheqFamilyId: Array(1)
0: "The EcheqFamilyId field is required."
length: 1
__proto__: Array(0)
__proto__: Object
如果我这样做:
console.log(this.selectedValueOptie);
它也是空的,我的意思是空白
【问题讨论】:
-
尝试将
value替换为[value]。希望在此更改之后它会正常工作。 -
您能否在stackblitz 中为您当前的场景创建一个示例,因为这将有助于社区更好地理解并以更好的方式解决您的问题。
标签: angular material-design dropdown observer-pattern selectedvalue