【发布时间】:2018-06-29 03:21:37
【问题描述】:
当我尝试编辑记录时,我无法设置mat-autocomplete 的值我将自动完成功能用作FormBuilder 对象中的FormControl 对象并设置从服务器接收的值我我正在使用 Formbuilder.setValue() 方法,但这并没有设置自动完成,尽管它向服务器发送了一个请求,因为我正在使用 Observables 的 valueChanges 方法......任何帮助将不胜感激。
以下是我正在使用的代码:
component.ts
this.filteredData = this.addDetailsForm.get('product').valueChanges
.debounceTime(400)
.do(value =>
{
let exist = this.myContent.findIndex(t => t.text === value);
if (exist > -1) return;
this._dataService.getSwiftProducts(value)
.subscribe((res: any[]) => { this.myContent = res; });
}).delay(500).map(() => this.myContent);
component.html
<div class="row">
<label class="col-lg-4 control-label">Product: </label>
<div class="col-lg-5">
<input type="text" class="form-control"
(keyup.enter)="chooseFirstOption()"
placeholder="Pick one" aria-label="Number"
matInput ="product" formControlName="product"
[matAutocomplete]="auto">
<p *ngIf="addDetailsForm.controls.product.errors">
This field is required!
</p>
<mat-autocomplete #auto="matAutocomplete"
[displayWith]="displayFn">
<mat-option *ngFor="let option of filteredData | async"
[value]="option">
{{ option.description }}
</mat-option>
</mat-autocomplete>
</div>
Angular、Material、OS、TypeScript 的版本:
Angular : "@angular/core": "^5.0.0",
Material : "@angular/material": "^5.0.0-rc0",
OS: Windows 7
Typescript : "typescript": "^2.6.2"
【问题讨论】: