Vip*_*ora 5 angular-material angular5 mat-autocomplete
我允许用户动态创建输入字段。对于每个输入字段,我想将其连接到不同的 mat-autocomplete,以便它们彼此独立工作。我在这里遇到了障碍,因为我无法动态创建将自动完成连接到输入的元素引用(此处为#auto)。我该如何实现这一目标?
<div
class="row"
*ngFor="let field of getControls('requestFields'); let i = index"
formArrayName="requestFields"
>
<ng-container [formGroupName]="i">
<div class="col-md-4">
<mat-form-field class="example-full-width">
<input
type="text"
placeholder="Name"
matInput
formControlName="reqName"
matAutocomplete="auto"
/>
<mat-autocomplete #auto="matAutocomplete">
<mat-option
*ngFor="let option of (filteredColumns | async)"
[value]="option"
>
{{ option }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
<div class="col-md-2">
<div class="togglebutton">
<label>
<span>Optional</span>
<input type="checkbox" formControlName="reqOption" />
<span class="toggle"></span>
</label>
</div>
</div>
<div class="col-md-4">
<mat-form-field>
<input
matInput
formControlName="reqValidations"
placeholder="Validation"
type="text"
/>
</mat-form-field>
</div>
</ng-container>
</div>
Run Code Online (Sandbox Code Playgroud)
一个好处mat-autocomplete是它与 完全解耦,mat-form-field因此您可以将它放在动态生成的行范围之外的任何位置。因此,以您的示例为例 - 解决方案可能如下所示:
<div
class="row"
*ngFor="let field of getControls('requestFields'); let i = index"
formArrayName="requestFields"
>
<ng-container [formGroupName]="i">
<div class="col-md-4">
<mat-form-field class="example-full-width">
<input
type="text"
placeholder="Name"
matInput
formControlName="reqName"
matAutocomplete="auto"
/>
</mat-form-field>
</div>
<!-- other dynamic content -->
</ng-container>
</div>
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredColumns | async" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
Run Code Online (Sandbox Code Playgroud)
keyup然后你可以在输入上有一个事件处理程序来触发更新filteredColumns
<mat-form-field class="example-full-width">
<input
type="text"
placeholder="Name"
matInput
formControlName="reqName"
matAutocomplete="auto"
(keyup)="reqNameChanged(field.get('reqName')?.value)"
/>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
在您的组件中,您可以设置filteredColumns一个由事件处理程序中的主题触发的可观察对象keyup:
import { Component, OnInit } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import {
debounceTime,
distinctUntilChanged,
filter,
switchMap
} from 'rxjs/operators';
@Component({
selector: 'example',
templateUrl: './example.html',
styleUrls: ['./example.scss']
})
export class ExampleComponent implements OnInit {
filteredColumns: Observable<string[]>;
reqNameSubject: Subject<string> = new Subject<string>();
constructor(private lookup: ILookupService) {}
ngOnInit() {
this.filteredColumns = this.reqNameSubject.pipe(
filter(v => !!v),
debounceTime(300),
distinctUntilChanged(),
switchMap(value =>
/* call for the autocomplete data */
this.lookup.search(value)
)
);
}
reqNameChanged(value: string) {
this.reqNameSubject.next(value);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望它有帮助。