feat: allow `MatFormFieldControl` to be content projected into `mat-form-field` using `ng-content`
Bug, feature request, or proposal:
Placeholder on input not floating when projected from ng-content.
What is the expected behavior?
Placeholder must floating, when input has some value.
What is the current behavior?
Placeholder looks like standard html placeholder, not like material placeholder.
What are the steps to reproduce?
StackBlitz: https://stackblitz.com/edit/angular-material2-issue-jekywv
What is the use-case or motivation for changing an existing behavior?
Inability to use mat input with content projection.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
latest
Is there anything else we should know?
In console i have an error: mat-form-field must contain a MatFormFieldControl.
FYI it's not just the placeholder that has issues, this is completely unsupported right now, hence the errors
Have the same issue. This feature will extremely help me to reduce boilerplate html. @mmalerba, please, fix it! Thx
News?
I ended up using material-design-lite as a temporary solution, hope this gets resolved soon.
Are there any related issues to this one? This seems like a very glaring oversight that should've been fixed by now.
ERROR Error: mat-form-field must contain a MatFormFieldControl. same error, all modules working well, except this.
Hi, any comments on the status of this issue?
Mh.. this complicates things. Not using ng-content but ngTemplateOutlet, but that the same story I guess.
There has a dirty method is setting the matformFieldControl in the custom field component.
StackBlitz: https://stackblitz.com/edit/angular-material2-issue-rewh7v?file=app/field.component.ts
This is a rather glaring limitation. Hopefully it gets addressed soon.
This worked for me:
https://stackoverflow.com/questions/50574642/angular-material-mat-form-field-custom-component-matsuffix-in-ng-content
I only get this error when I add the basic native select, but other work fine.
Waiting clear solutions...more than a year
"Waiting clear solutions...more than a year" me too!
ping @mmalerba
Is resolving this a consideration atm?
You need to import the MatInputModule into your app.module.ts file
A somewhat crude solution I found to bypass the issue
<mat-form-field>
<input matInput [hidden]="true">
<ng-container [ngTemplateOutlet]="content"></ng-container>
</mat-form-field>
Seems to work, although you should test carefully
You need to import the MatInputModule into your app.module.ts file
import { MatInputModule} from '@angular/material'; works fine for me thanks @claytoncc
I got this work by:
<mat-form-field>
<input matInput hidden>
<ng-content></ng-content>
</mat-form-field>
I didn't managed to make @qwe852147's solution work with version 9. So I came up with an even dirtier solution: html:
<mat-form-field>
<ng-content></ng-content>
<!-- Placeholder control to fool mat-form-field -->
<ng-container *ngIf="beforeViewInit">
<input matInput/>
</ng-container>
</mat-form-field>
typescript:
export class FormFieldComponent implements AfterViewInit
beforeViewInit = true; // Used to remove placeholder control
@ContentChild(MatFormFieldControl) control: MatFormFieldControl<any>;
@ViewChild(MatFormField) matFormField: MatFormField;
constructor(private cdr: ChangeDetectorRef) {}
ngAfterViewInit() {
if (this.beforeViewInit) {
this.matFormField._control = this.control;
this.beforeViewInit = false;
this.cdr.detectChanges();
}
}
}
I just hope this issue will be addressed at some point to avoid this ugly / suboptimzed solution.