Reputation: 2594
I'm mastering angular directive and I'd like to pass a value to my component my code:
My component
`
@Component({
selector: 'app-my-one',
template: `
<ng-container
*ngTemplateOutlet="layoutTemplate; context: { name: name }"
></ng-container>
`
})
export class MyOneComponent {
@ContentChild(TemplateRef, { static: true })
@Input()
layoutTemplate: TemplateRef<any>;
}
`
and in app.component.html
`@Component({
selector: 'app-root',
template: `
<app-my-one>
<ng-template let-name="current">
<span>Ahoj {{ name }}!</span>
</ng-template>
</app-my-one>
`
})
export class AppComponent {
current = 'Svet';
}`
But I can only see 'Ahoj'
Can you help me please?
Upvotes: 1
Views: 2452
Reputation: 24414
try this way by pass the context object to the MyOneComponent
MyOneComponent template
<ng-container
*ngTemplateOutlet="layoutTemplate;context:myContext"
></ng-container>
App Component template
<app-my-one [myContext]="current" >
<ng-template let-name="name">
<span>Ahoj {{ name }}!</span>
</ng-template>
</app-my-one>
Upvotes: 1