gpt4 book ai didi

angular - Mat-Autocomplete formControlName

转载 作者:行者123 更新时间:2023-12-04 12:42:52 25 4
gpt4 key购买 nike

我在 Mat-Autocomplete 上使用 formControlName 时遇到问题。
我不知道为什么,但我的 formControlName 不会发送数据,是我写错了吗?
当我尝试从 HTML 中删除 [formControl] 时,它总是无法过滤。

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 是我获取银行列表的地方,_validationService 是我获取每个表单名称验证的地方。
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())
);
})
}

最佳答案

首先,初始化表单控件是很好的:

this.form = this.formBuilder.group({
bankName : [bankName, Validators.required]
});


this.form = this.formBuilder.group({
bankName: new FormControl(bankName, Validators.required)
});

并解决问题
<input type="text" placeholder="Pilih Bank" 
formControlName="bankName" matInput
[formControl]="myControl" [matAutocomplete]="auto">


<input type="text" placeholder="Pilih Bank" matInput 
[formControl]="form.get('bankName')" [matAutocomplete]="auto">

它应该可以解决您的问题。

关于angular - Mat-Autocomplete formControlName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56073570/

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