Given a root component without properties, this root component template would give an error:
<div>My name is {{foo}}.</div>
I want to create a component (FooProvider) which projects its content, but furthermore provides a binding for 'foo', such that I can write in my root component:
<foo-provider>
<div>My name is {{foo}}.</div>
</foo-provider>
In other words: how can I put bindings into <ng-content> ?
Update:
Trying this as a structural directive also does not work. What is wrong here? (Angular 5.2.11)
This the root component:
<ng-container *foo-provider>
<div>foo = {{foo}}</div>
</ng-container>
This is the FooProviderDirective:
import {Directive, TemplateRef, ViewContainerRef} from '@angular/core';
interface FooContext {
foo: string;
}
@Directive({
selector: '[foo-provider]'
})
export class FooProviderDirective {
constructor(
private readonly viewRef: ViewContainerRef,
private readonly templateRef: TemplateRef<FooContext>
) {
const context: FooContext = { foo: 'baz' };
this.viewRef.createEmbeddedView(this.templateRef, context);
}
}