簡體   English   中英

如何清除材料自動完成 angular 中的所有加載值?

[英]How to clear all loaded values in material autocomplete angular?

我正在嘗試在另一個 mat-select 字段的 selectionchange 上清除材料輸入字段中的所有加載值。 我只能清除選定的值,而不能清除輸入字段property中所有加載的值。 這是代碼。

HTML

<mat-form-field>
    <mat-select formControlName="searchType" (selectionChange)="searchTypeChange()" placeholder="Search By">
        <mat-option *ngFor="let st of searchTypes" [value]="st.value">
            {{st.viewValue}}
        </mat-option>
    </mat-select>
</mat-form-field>

<mat-form-field *ngIf="searchForm.get('searchType').value==6" class="example-full-width">
    <input matInput placeholder="PropertyID" [formControl] ="property"    [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete" autoActiveFirstOption>
        <mat-option *ngFor="let option of propertyfilteredOptions | async" [value]="option">
            {{option}}
        </mat-option>
    </mat-autocomplete>
</mat-form-field>

Typescript

ngOnInit(): void {
    this.searchForm=this.fb.group({
       prop:this.property,
    });
}
searchTypeChange(){
    this.searchForm.get('prop').setValue('');
    this.property.reset(); //this is not working
}

您正在嘗試重置 FormControl。 它不向 MatSelect 提供數據。 它從訂閱中獲取數據 - propertyfilteredOptions | async propertyfilteredOptions | async 您需要將新值傳遞給propertyfilteredOptions Observable。

你也可以 f.ex. 只需將其更改為新的 Observable:

this.propertyfilteredOptions = new Observable([]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM