【发布时间】:2020-02-23 22:52:57
【问题描述】:
我想使用自动完成输入进行搜索,但自动完成结果未绑定在表单输入搜索按钮上。尽管我给出了一些自动完成结果(搜索按钮仍然被禁用),但看起来输入文本是原始的。
<form [formGroup]="searchForm">
<section class="form-block">
<div class="form-group" id="customerIdFormGroup">
<label class="required" for="customerIdInput">Customer ID</label>
<input type="text"
id="customerIdInput"
(keyup.enter)="emitSearch()"
formControlName="customerId"
matInput
[formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of (filteredOptions | async)" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
</div>
<button type="button"
id="searchButton"
class="btn btn-primary"
[disabled]="searchForm.pristine"
(click)="emitSearch()">
Search
</button>
<button type="reset"
id="clearButtonInvoice"
class="btn btn-link text-primary"
(click)="emitClear()">
Clear
</button>
</section>
</form>
我使用 angular materiel 7.3.1 和 .
如果我删除matInput 部分,搜索按钮效果很好,
[formControl]="myControl"
[matAutocomplete]="auto"
但没有自动完成功能。
<form [formGroup]="searchForm">
<section class="form-block">
<div class="form-group" id="customerIdFormGroup">
<label class="required" for="customerIdInput">Customer ID</label>
<input type="text"
id="customerIdInput"
(keyup.enter)="emitSearch()"
formControlName="customerId"
matInput
[formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of (filteredOptions | async)" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
</div>
<button type="button"
id="searchButton"
class="btn btn-primary"
[disabled]="searchForm.pristine"
(click)="emitSearch()">
Search
</button>
<button type="reset"
id="clearButtonInvoice"
class="btn btn-link text-primary"
(click)="emitClear()">
Clear
</button>
</section>
</form>
我希望我可以使用自动完成输入进行搜索。给出一些输入文本后,搜索按钮应该被激活,我可以使用文本进行搜索。
【问题讨论】:
-
导出类 SearchByCustomerIdComponent 实现 OnInit { myControl = new FormControl();选项:字符串[] = ['N3RSRCORP', 'N0KINGKULL', 'N3CACTUS', 'N3RCACORP', 'N4UPS' , 'N4UPSAMER' , 'N4UNISTAR' , 'N4UPS', 'N4UPSTRUCk'];过滤选项:Observable
; @Input() 搜索表单:表单组; @Output() searchByCustomerId: EventEmitter = new EventEmitter(); @Output() 清除:EventEmitter = new EventEmitter(); emitSearch() { this.searchByCustomerId.emit(); }
标签: html angular input autocomplete angular-material