垫自动完成显示与秋田始终相同的名称

时间:2018-09-01 16:01:05

标签: angular angular-material akita angular-akita

我的表格垫自动完成有问题。当我在该字段中写入内容时,它显示的是我数据库中具有相同名称的最后一个条目的两倍:

example

database

我在项目中使用Akita - State Management for Angular,但找不到此问题的根本原因。也许你可以帮我吗?

这是我的代码:

assistants-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>

assistants-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();
  }

nationalities-data.service.ts

export class NationalitiesDataService {

  /* Some code */

  get(): Observable<Nationality[]> {
    return this.http.get<Nationality[]>(this.url);
  }

有什么输入可以解决这个问题吗?

预先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

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

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

您可以定义自定义ID密钥,如下所示:

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

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

签出docs