mat-autocomplete 始终显示与 Akita 相同的名称

mat-autocomplete displaying always the same name with Akita

我的表单自动填充功能有问题。当我在字段中写东西时,它会显示我数据库中最后一个同名条目的两倍:

我在我的项目中使用 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;
  }

nationalities.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);
  }

有解决这个问题的意见吗?

在此先感谢您的帮助

我能想到的唯一原因是您的 id 密钥与 id 不同。例如:

entity = { id: 1, title: '' }

您可以定义自定义 id 键,如下所示:

@StoreConfig( { name: '', idKey: 'todoId' } )
class TodosStore {}

entity = { todoId: 1, title: '' }

查看 docs