New*_*iie 6 autocomplete filter angular-material angular
我正在使用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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
(如果我正确理解了问题)
您在被管道FormGroup的.valueChanges。但是您需要在上执行此操作FormControl。
所以代替
this.filteredOptions = this.tumeurForm.valueChanges.pipe(
startWith(""),
map(val => this.filter(val))
);
Run Code Online (Sandbox Code Playgroud)
做这个:
this.filteredOptions = this.tumeurForm.controls['localisation'].valueChanges.pipe(
startWith(""),
map(val => this.filter(val))
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2929 次 |
| 最近记录: |