I want to create a "my-form-group" component, consisting of a label, any type of input element (input, checkbox, ...) and a div for validation results. I want to use content projection to insert the input after the label. Something like this:
<form [ngFormModel]="myForm" (ngSubmit)="onSubmit()">
<my-form-group>
<input type="text"
class="form-control"
[ngFormControl]="myForm.controls['name']">
</my-form-group>
<form>
The component could look like this:
@Component({
selector: 'my-form-group',
template: `
<div class="form-group">
<label for="name">Name<span [ngIf]="name.required"> *</span></label>
<ng-content></ng-content>
<div [hidden]="name.valid || name.pristine" class="alert alert-danger">
Please check your input
</div>
</div>`
})
...
I want to use the state of the projected component to hide or show the "required" asterisk and the validation div. As far as I know, a projected component can be accessed by using @ContentChild() and in ngAfterContentInit(), but I think, I must have an special component to use this.
What is the best way to access the controller of the projected component, if I don't know the exact component?