I want to pass some properties from a parent component to a child component using content projection, is that possible?
For e.g. this is my template:
<my-form [display]="'horiz'">
Email: <my-input [type]="'email'" ...></my-input>
Name: <my-input [type]="'name'" [display]="'vert'" ...></my-input>
...
</my-form>
Now the my-form
component has the template like this:
<form ...>
<ng-content></ng-content>
</form>
What I want is that the display
property of my-form
can be accessed by the my-input
components, such that it can be overriden by the my-input
component as well, like it is for the Name input.
Is that possible?
To let Angular know that a property in a child component or directive can receive its value from its parent component we must use the @Input() decorator in the said child. The @Input() decorator allows data to be input into the child component from a parent component.
Otherwise, remember the three steps: Prepare Child component to emit data. Bind Property in Parent Component template. Use Property in Parent Component class.
You can use @ContentChildren
, made an example on stackblitz:
https://stackblitz.com/edit/angular-content-children-example
I have modified the values in the ngAfterViewInit
using the component references from the ContentChildren
.
Hope it helps. Feel free to update the code if any better approach is there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With