I have issue using formControlName on Mat-Autocomplete. i dont know why but my formControlName wont send the data, am i put it wrong? When i try to remove [formControl] from HTML, its always cant filter.
HTML
<form [formGroup]="form" #legalDataFrm="ngForm" autocomplete="off" fxLayout.gt-sm="column" fxFlex="1 1 auto" novalidate>
<input type="text" placeholder="Pilih Bank"
formControlName="bankName" matInput
[formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete
#auto="matAutocomplete"
(optionSelected)='onChangeBank()'
[displayWith]="displayFn">
<mat-option>Pilih Bank</mat-option>
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option.bankName}}
</mat-option>
</mat-autocomplete>
</form>
TS _bankService is where i get the Bank List, _validationService is where i get the validation for each form name.
export class FpLegalDataFormComponent implements OnInit, OnDestroy {
form: FormGroup;
myControl = new FormControl();
options: User[];
filteredOptions: Observable<User[]>;
constructor(
private activatedRoute: ActivatedRoute,
private formBuilder: FormBuilder,
public _validationsService: ValidationsService,
public _bankService: BankService
) {
}
this.form = this.formBuilder.group({
bankName : [bankName, Validators.required]
});
displayFn(user?: User): string | undefined {
return user ? user.bankName : undefined;
}
private _filter(name: string): User[] {
const filterValue = name.toLowerCase();
return this.options.filter(option =>
option.bankName.toLowerCase().indexOf(filterValue) > -1);
}
private initDropdown() {
this._bankService.getBank().then((response) => {
console.log(response.data.bankList)
this.options = response.data.bankList;
console.log(this.options)
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith<string | User>(''),
map(value => typeof value === 'string' ? value : value.bankName),
map(name => name ? this._filter(name) : this.options.slice())
);
})
}
First of all it's good to initialize the form control so insted of:
this.form = this.formBuilder.group({
bankName : [bankName, Validators.required]
});
use
this.form = this.formBuilder.group({
bankName: new FormControl(bankName, Validators.required)
});
and to fix the problem insted of
<input type="text" placeholder="Pilih Bank"
formControlName="bankName" matInput
[formControl]="myControl" [matAutocomplete]="auto">
use
<input type="text" placeholder="Pilih Bank" matInput
[formControl]="form.get('bankName')" [matAutocomplete]="auto">
it should fix your problem.
TS:
public form: FormGroup = this.formBuilder.group({
control: [undefined, [Validators.required]],
});
//
this.filtered = this.signupForm.controls.control.valueChanges.pipe(...);
HTML:
<mat-form-field>
<input
type="text"
matInput
[matAutocomplete]="auto"
formControlName="control"
>
<mat-autocomplete
autoActiveFirstOption
#gymAuto="matAutocomplete"
(optionSelected)="form.controls.control.setValue($event.option.value)"
>
<mat-option
*ngFor="let option of filtered | async"
[value]="option"
>
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With