Angular ng-content nie działa z mat-form-field

Sep 15 2020

Mój cel:

Próbuję zbudować wielokrotnego użytku pole-formularz z wyraźnym przyciskiem.

Jak próbowałem osiągnąć swój cel:

Utworzyłem komponent „mat-clear-input-input” i użyłem go w następujący sposób:

<mat-clearable-input>
        <mat-label>Put a Number here pls</mat-label>
        <input matInput formControlName="number_form_control">
    </mat-clearable-input>

mat-clearable-input.component.html

<mat-form-field>
    <ng-content></ng-content>
</mat-form-field>

Oczekiwany wynik :

tag ng-content pobiera etykietę i dane wejściowe i umieszcza je wewnątrz tagu mat-form-field.

Rzeczywisty wynik :

Error: mat-form-field must contain a MatFormFieldControl.
    at getMatFormFieldMissingControlError (form-field.js:226)
    at MatFormField._validateControlChild (form-field.js:688)
    at MatFormField.ngAfterContentChecked (form-field.js:558)
    at callHook (core.js:2926)
    at callHooks (core.js:2892)
    at executeInitAndCheckHooks (core.js:2844)
    at refreshView (core.js:7239)
    at refreshComponent (core.js:8335)
    at refreshChildComponents (core.js:6991)
    at refreshView (core.js:7248)

Wygląda na to, że czegoś mi brakuje i nie używam poprawnie tagu ng-content.

Nie mogłem znaleźć dokumentacji dla tagu ng-content w witrynie kątowej.

Dziękuję za pomoc.

EDYTUJ PO ODPOWIEDZI PONIŻEJ

Wypróbowałem więc tę sugerowaną metodę:

export class MatClearableInputComponent implements OnInit {
  @ContentChild(MatFormFieldControl) _control: MatFormFieldControl<any>;
  @ViewChild(MatFormField) _matFormField: MatFormField;
  // see https://stackoverflow.com/questions/63898533/angular-ng-content-not-working-with-mat-form-field/
  ngOnInit() {
    this._matFormField._control = this._control;
  }

}

niestety, gdy próbuję użyć tego w formie, nadal kończy się to niepowodzeniem i pojawia się błąd „Błąd: pole-formularza-mat musi zawierać element MatFormFieldControl”.

Kod, w którym próbuję użyć tego komponentu w postaci:

<mat-clearable-input>
    <mat-label>Numero incarico</mat-label>
    <buffered-input matInput formControlName="numero"></buffered-input>
</mat-clearable-input>

Repro na stackblitz: https://stackblitz.com/edit/angular-material-starter-xypjc5?file=app/clearable-form-field/clearable-form-field.component.html

zwróć uwagę, że funkcje mat-form-field nie działają (bez konturu, bez pływającej etykiety), otwórz także konsolę, a zobaczysz błąd Error: mat-form-field musi zawierać MatFormFieldControl.

EDYTUJ PO WYSŁANIU OPCJI 2

Próbowałem to zrobić:

<mat-form-field>
  <input matInput hidden>
  <ng-content></ng-content>
</mat-form-field>

Działa, ale kiedy dodałem etykietę mat do mojego pola formularza, na przykład:

<mat-clearable-input>
        <mat-label>Numero incarico</mat-label>
        <buffered-input matInput formControlName="numero"></buffered-input>
    </mat-clearable-input>

etykieta nigdy się nie zmienia i po prostu pozostaje tam przez cały czas.

Więc próbowałem przypisać do this._matFormField._control._labeltreści podrzędnej z etykietą, ale to nie zadziałało, ponieważ _label jest prywatna i nie ma dla niej ustawiacza.

Wygląda na to, że nie mam szczęścia i nie można tego zrobić w Angular bez dużego wysiłku.

Jeśli masz jeszcze jakieś pomysły, nie krępuj się rozwalić stackblitz i spróbować!

Edytuj po odpowiedzi @evilstiefel

rozwiązanie działa tylko dla natywnych <input matInput>. Kiedy próbuję zastąpić to moim niestandardowym komponentem wejściowym, już nie działa.

Przygotowanie do pracy:

<mat-form-field appClearable>
    <mat-label>ID incarico</mat-label>
    <input matInput formControlName="id">
</mat-form-field>

Taka sama konfiguracja, ale z moim niestandardowym składnikiem „buforowane wejście” (nie działa :()

<mat-form-field appClearable>
    <mat-label>ID incarico</mat-label>
    <buffered-input matInput formControlName="id"></buffered-input>
</mat-form-field>

Konsola rejestruje ten błąd po kliknięciu przycisku wyczyść:

TypeError: Cannot read property 'ngControl' of undefined
    at ClearableDirective.clear (clearable.directive.ts:33)
    at ClearButtonComponent.clearHost (clearable.directive.ts:55)
    at ClearButtonComponent_Template_button_click_0_listener (clearable.directive.ts:47)
    at executeListenerWithErrorHandling (core.js:14293)
    at wrapListenerIn_markDirtyAndPreventDefault (core.js:14328)
    at HTMLButtonElement.<anonymous> (platform-browser.js:582)
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Object.onInvokeTask (core.js:27126)
    at ZoneDelegate.invokeTask (zone-evergreen.js:398)
    at Zone.runTask (zone-evergreen.js:167)

Odpowiedzi

1 evilstiefel Sep 29 2020 at 09:16

Innym rozwiązaniem jest użycie dyrektywy do zaimplementowania zachowania.

import {
  AfterViewInit,
  Component,
  ComponentFactory,
  ComponentFactoryResolver,
  ContentChild,
  Directive,
  Injector,
  Input,
  Optional,
  SkipSelf,
  TemplateRef,
  ViewContainerRef,
} from '@angular/core';
import { MatFormFieldControl } from '@angular/material/form-field';


@Directive({
  selector: '[appClearable]'
})
export class ClearableDirective implements AfterViewInit {

  @ContentChild(MatFormFieldControl) matInput: MatFormFieldControl<any>;
  @Input() appClearable: TemplateRef<any>;
  private factory: ComponentFactory<ClearButtonComponent>;

  constructor(
    private vcr: ViewContainerRef,
    resolver: ComponentFactoryResolver,
    private injector: Injector,
  ) {
    this.factory = resolver.resolveComponentFactory(ClearButtonComponent);
  }

  ngAfterViewInit(): void {
    if (this.appClearable) {
      this.vcr.createEmbeddedView(this.appClearable);
    } else {
      this.vcr.createComponent(this.factory, undefined, this.injector);
    }
  }

  /**
   * This is used to clear the formControl oder HTMLInputElement
   */
  clear(): void {
    if (this.matInput.ngControl) {
      this.matInput.ngControl.control.reset();
    } else {
      this.matInput.value = '';
    }
  }
}

/**
 * This is the markup/component for the clear-button that is shown to the user.
 */
@Component({
  selector: 'app-clear-button',
  template: `
  <button (click)="clearHost()">Clear</button>
  `
})
export class ClearButtonComponent {
  constructor(@Optional() @SkipSelf() private clearDirective: ClearableDirective) { }

  clearHost(): void {
    if (this.clearDirective) {
      this.clearDirective.clear();
    }
  }
}

Spowoduje to utworzenie dyrektywy o nazwie appClearablei opcjonalnego składnika dla układu rezerwowego. Upewnij się, że dodałeś komponent i dyrektywę do declarations-array swojego modułu. Możesz określić szablon, który ma być używany w celu zapewnienia interfejsu użytkownika, lub po prostu użyć go ClearButtonComponentjako rozwiązania uniwersalnego. Znacznik wygląda następująco:

<!-- Use it with a template reference -->
<mat-form-field [appClearable]="clearableTmpl">
  <input type="text" matInput [formControl]="exampleInput">
</mat-form-field>

<!-- use it without a template reference -->
<mat-form-field appClearable>
  <input type="text" matInput [formControl]="exampleInput2">
</mat-form-field>

<ng-template #clearableTmpl>
  <button (click)="exampleInput.reset()">Marked-Up reference template</button>
</ng-template>

Działa to zi bez ngControl / FormControl, ale może być konieczne dostosowanie go do twojego przypadku użycia.

2 Akash Sep 24 2020 at 12:46

Aktualizacja :

Opcja 1 nie działa dla nowych wersji kątowych, ponieważ @ViewChild()zwraca wartość undefined w ngOnInit()hooku. Kolejnym hackem jest użycie atrapy MatFormFieldControl-

Opcja 2

<mat-form-field>
  <input matInput hidden>
  <ng-content></ng-content>
</mat-form-field>

Edycja :

Ten błąd jest generowany, ponieważ MatFormFieldkomponent wysyła zapytanie do treści podrzędnej, @ContentChild(MatFormFieldControl)która nie działa, jeśli używasz zagnieżdżonego ng-content( MatFormField również używa projekcji treści ).

Opcja 1 (wycofana)

Poniżej przedstawiamy, jak możesz to zrobić -

@Component({
  selector: 'mat-clearable-input',
  template: `
    <mat-form-field>
      <ng-content></ng-content>
    </mat-form-field>
  `
})
export class FieldComponent implements OnInit { 
    @ContentChild(MatFormFieldControl) _control: MatFormFieldControl<any>;
    @ViewChild(MatFormField) _matFormField: MatFormField;

    ngOnInit() {
        this._matFormField._control = this._control;
    }
}

Proszę sprawdzić ten stackBlitz . Ponadto, nie jest to problem stworzony w githubjuż.