gpt4 book ai didi

angular - FormGroup CustomFilter 中的 Mat-AutoComplete

转载 作者:太空狗 更新时间:2023-10-29 17:34:30 31 4
gpt4 key购买 nike

我正在使用 Angular CLI。我想制作一个自动完成表单,查看所有值(不像 angular.io 的例子那样做一个“开始于”)。

我设法让它与 [formControl] 一起工作,但我想将它插入到 FormGroup 中。所以我认为将它与 formControlName 一起使用(同时使用 formControlName[formControl] )意味着我没有从我的形式。

这是我当前的代码,但过滤器存在问题。谢谢你的帮助

component.html:

<form [formGroup]="tumeurForm" (ngSubmit)="onSubmitForm()">
<mat-form-field appearance="outline">
<mat-label>Diagnostic : inscription de la tumeur</mat-label>
<input
matInput
type="text"
formControlName="localisation"
[matAutocomplete]="auto"/>
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>

component.ts:

export class DiagnosticDialogComponent implements OnInit  {

options = [
"(C00) Néoplasie maligne de la lèvre",
"(C00.0) Lèvre supérieure, bord libre",
"(C00.1) Lèvre inférieure, bord libre"
];

patientid: string;
public tumeurForm: FormGroup ;
filteredOptions: Observable<string[]>;


constructor(private formBuilder: FormBuilder) { }

ngOnInit() {
this.initForm();
this.filteredOptions = this.tumeurForm.valueChanges.pipe(
startWith(""),
map(val => this.filter(val))
);
}

filter(val: string): string[] {
return this.options.filter(option => {
return option.toLowerCase().match(val.toLowerCase());
});
}

initForm() {
this.tumeurForm = this.formBuilder.group({
localisation: ['', Validators.required]
});
}

onSubmitForm() {
const localisation = this.tumeurForm.get('localisation').value;
const Patientid = this.patientid;
const newDiagnostic = new Diagnostic(localisation, Patientid);
this.diagnosticsService.CreateDiagnostic(newDiagnostic);
}
}

最佳答案

(如果我理解正确的话)

您正在对 FormGroup.valueChanges 进行管道传输。但是您需要在 FormControl 上执行此操作。

所以代替

this.filteredOptions = this.tumeurForm.valueChanges.pipe(
startWith(""),
map(val => this.filter(val))
);

这样做:

this.filteredOptions = this.tumeurForm.controls['localisation'].valueChanges.pipe(
startWith(""),
map(val => this.filter(val))
);

关于angular - FormGroup CustomFilter 中的 Mat-AutoComplete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50928851/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com