I'm looking for solution to pass current component scope data into ng-content
directive.
I've app-table
component template with data I want to pass into child content, using some solution like this:
<!-- some html before --> <table> <!-- complex header markup with sorting/filtering support --> <tr *ngFor="let item of data"> <ng-content [value]="item"></ng-content> //how to pass data? </tr> </table> //some html after
And page template where I want to use this data:
<app-table> <td>{{value.Name}}</td> ... </app-table>
Is this possible?
There are two ways to pass data into a component, with 'property binding' and 'event binding'. In Angular, data and event change detection happens top-down from parent to children. However for Angular events we can use the DOM event mental model where events flow bottom-up from child to parent.
Use the :host /deep/ selector. If you want to use the shadow DOM polyfill and style your components using the style or styleUrls attribute of the Component decorator, select the element with :host , then ignore the shadow DOM polyfill with the /deep/ child selector. :host will select the element.
I think you could find your answer here: Angular 2 passing html to ng-content with bindings.
The ng-content component is badly documented, unfortunately. It will be soon, according to the Angular GitHub issue (https://github.com/angular/angular.io/issues/3099).
I faced the same issue, for more clarification, I added your example working.
<!-- some html before --> <table> <!-- complex header markup with sorting/filtering support --> <tr *ngFor="let item of data"> <ng-template [ngTemplateOutlet]="templateRef" [ngTemplateOutletContext]="{$implicit: item}"></ng-template> </tr> </table> //some html after
In the Component, you need to declare templateRef
like this.
export class AppTableComponent { ... @ContentChild(TemplateRef) templateRef: TemplateRef<any>; ... }
Then you use your component like this.
<app-table> <ng-template let-item> <td>{{item.Name}}</td> ... </ng-template> </app-table>
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