angular / angular/components

Allow mat-error in mat-form-field subviews

Aperta
#15,778 0 commenti 11 reazioni 0 assegnatari Vedi su GitHub
area: material/form-field P3
Lingua principale
TypeScript
Stelle
25k
Fork
6.8k
Merge medio
1g 8h
PR unite (30g)
91

Descrizione

#### What is the expected behavior?
**NOTE**: it's more of an improvement (but a really critical one) than a bug.

`mat-error` should work even if it's not used as direct child of `mat-form-field`. It should work if it's used inside a component which itself is used in `mat-form-field`.

This is particularly necessary for **handling error messages in a reusable component**.
A real-world application may have several forms with a lot of validators on their fields.
Repeating a bunch of `mat-error`s and corresponding error messages for each usecase is just tedious.

### Suggested solution
I think the reason this currently doesn't work is because `@ContentChildren` decorator is used to get a hold of `mat-error` instances. However `@ContentChildren` [doesn't work for nested components](https://github.com/angular/angular/issues/5241) and aparently [this is by design](https://github.com/angular/angular/issues/5241#issuecomment-156841935).
Instead of using `@ContentChildren` , `MatError` can inject `MatFormField` and register itself into it.

#### What is the current behavior?
`mat-error` only works if it's directly used inside `mat-form-field`. If it's used inside a nested component, it doesn't work as expected.

#### What are the steps to reproduce?
https://stackblitz.com/angular/kllvoljyoem

#### Which versions of Angular, Material, OS, TypeScript, browsers are affected?
@angular/cdk@7.3.0
@angular/material@7.3.0
Angular: 7.2.3

#### Is there anything else we should know?

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia con la riproduzione StackBlitz collegata e analizza come mat-form-field rileva le istanze di mat-error attraverso un componente riutilizzabile annidato. Conferma l'attuale limitazione ai figli diretti, quindi verifica che gli errori di convalida vengano visualizzati quando mat-error è annidato e che il comportamento esistente per i figli diretti rimanga invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
angular, typescript
Ambito
frontend
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.