4

我允许用户动态创建输入字段。对于这些输入字段中的每一个,我想将其连接到不同的垫子自动完成功能,以便它们彼此独立工作。我在这里碰壁了,因为我无法动态创建将自动完成连接到输入的元素引用(#auto here)。我如何实现这一目标?

<div
  class="row"
  *ngFor="let field of getControls('requestFields'); let i = index"
  formArrayName="requestFields"
>
  <ng-container [formGroupName]="i">
    <div class="col-md-4">
      <mat-form-field class="example-full-width">
        <input
          type="text"
          placeholder="Name"
          matInput
          formControlName="reqName"
          matAutocomplete="auto"
        />
        <mat-autocomplete #auto="matAutocomplete">
          <mat-option
            *ngFor="let option of (filteredColumns | async)"
            [value]="option"
          >
            {{ option }}
          </mat-option>
        </mat-autocomplete>
      </mat-form-field>
    </div>
    <div class="col-md-2">
      <div class="togglebutton">
        <label>
          <span>Optional</span>
          <input type="checkbox" formControlName="reqOption" />
          <span class="toggle"></span>
        </label>
      </div>
    </div>
    <div class="col-md-4">
      <mat-form-field>
        <input
          matInput
          formControlName="reqValidations"
          placeholder="Validation"
          type="text"
        />
      </mat-form-field>
    </div>
  </ng-container>
</div>
4

1 回答 1

3

一个好处mat-autocomplete是它与 完全解耦,mat-form-field因此您可以将它放在动态生成的行范围之外的任何地方。因此,以您为例-解决方案可能如下所示:

<div
  class="row"
  *ngFor="let field of getControls('requestFields'); let i = index"
  formArrayName="requestFields"
>
  <ng-container [formGroupName]="i">
    <div class="col-md-4">
      <mat-form-field class="example-full-width">
        <input
          type="text"
          placeholder="Name"
          matInput
          formControlName="reqName"
          matAutocomplete="auto"
        />
      </mat-form-field>
    </div>
   <!-- other dynamic content -->
  </ng-container>
</div>
<mat-autocomplete #auto="matAutocomplete">
  <mat-option *ngFor="let option of filteredColumns | async" [value]="option">
    {{ option }}
  </mat-option>
</mat-autocomplete>

keyup然后你可以在触发更新的输入上有一个事件处理程序filteredColumns

<mat-form-field class="example-full-width">
  <input
    type="text"
    placeholder="Name"
    matInput
    formControlName="reqName"
    matAutocomplete="auto"
    (keyup)="reqNameChanged(field.get('reqName')?.value)"
  />
</mat-form-field>

在您的组件中,您可以设置一个由事件处理程序filteredColumns的主题触发的可观察对象:keyup

import { Component, OnInit } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import {
  debounceTime,
  distinctUntilChanged,
  filter,
  switchMap
} from 'rxjs/operators';

@Component({
  selector: 'example',
  templateUrl: './example.html',
  styleUrls: ['./example.scss']
})
export class ExampleComponent implements OnInit {
  filteredColumns: Observable<string[]>;

  reqNameSubject: Subject<string> = new Subject<string>();

  constructor(private lookup: ILookupService) {}

  ngOnInit() {
    this.filteredColumns = this.reqNameSubject.pipe(
      filter(v => !!v),
      debounceTime(300),
      distinctUntilChanged(),
      switchMap(value =>
        /* call for the autocomplete data */
        this.lookup.search(value)
      )
    );
  }

  reqNameChanged(value: string) {
    this.reqNameSubject.next(value);
  }
}

我希望它有所帮助。

于 2018-11-12T02:25:20.767 回答