我的表单的垫子自动完成有问题。当我在该字段中写一些东西时,它显示了我数据库中最后一个同名条目的两倍:
我在我的项目中使用Akita - State Management for Angular,但我找不到这个问题的根本原因。也许你能帮助我?
这是我的代码:
助手-page.component.html
<!-- Some code -->
<!-- Nationality -->
<ng-container *ngIf="!(loading$ | async); else loadingTpl">
<mat-form-field>
<input matInput type="text" placeholder="Nationalité" formControlName="nationality" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
<mat-option *ngFor="let nationality of nationalities$ | async" [value]="nationality">
{{ nationality.name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</ng-container>
<ng-template #loadingTpl>Loading...</ng-template>
助手-page.component.ts
export class AssistantsPageComponent implements OnInit, OnDestroy {
/* Some code */
loading$: Observable<boolean>;
nationalities$: Observable<Nationality[]>;
constructor(
private assistantsQuery: AssistantsQuery,
private assistantsService: AssistantsService,
private fb: FormBuilder,
private nationalitiesQuery: NationalitiesQuery,
private nationalitiesService: NationalitiesService,
) { }
ngOnInit() {
this.formGroup = this.fb.group({
title: ['', Validators.required],
lastName: ['', Validators.required],
firstName: ['', Validators.required],
nationality: null
});
this.nationalitiesService.get().subscribe();
this.loading$ = this.nationalitiesQuery.selectLoading();
this.nationalities$ = this.formGroup.get('nationality').valueChanges.pipe(
switchMap(value => this.nationalitiesQuery.selectAll({
filterBy: entity => entity.name.toLowerCase().includes(value)
}))
);
this.persistForm = new PersistNgFormPlugin(this.assistantsQuery, createAssistant).setForm(this.formGroup);
}
displayFn(nationality?: Nationality): string | undefined {
return nationality ? nationality.name : undefined;
}
国籍.service.ts
export class NationalitiesService {
/* Some code */
get(): Observable<Nationality[]> {
const request = this.nationalitiesDataService.get().pipe(
tap(response => this.nationalitiesStore.set(response)
));
return this.nationalitiesQuery.isPristine ? request : noop();
}
国籍-data.service.ts
export class NationalitiesDataService {
/* Some code */
get(): Observable<Nationality[]> {
return this.http.get<Nationality[]>(this.url);
}
任何输入来解决这个问题?
在此先感谢您的帮助