This is not possible using <ng-content>.
However, you can achieve it by projecting templates instead of regular content.
Your component will then render these templates instead of ng-content via <ng-container>'s marked with an ngTemplateOutlet and set a context for these templates.
By using structural directives (adding a * in front of your directive), you are effectively wrapping your injected content with templates.
By using @ContentChild in your component, you can then reference these templates to instantiate them.
In order to access the template context when using your component, you can use the let keyword on the template to alias the implicit context and then reference it.
<div class="measure">
<ng-container *ngTemplateOutlet="labelTemplate; context: templateContext"></ng-container>
<div class="chart">
<div class="state" [style.width.%]="progress"> </div>
</div>
<ng-container *ngTemplateOutlet="valueTemplate; context: templateContext"></ng-container>
</div>
export class ProgressComponent implements OnInit {
@Input() current: any;
@Input() maximum: any;
@Input() minimum: any;
@ContentChild(LabelDirective, { read: TemplateRef }) labelTemplate;
@ContentChild(ValueDirective, { read: TemplateRef }) valueTemplate;
progress: number;
templateContext = { $implicit: this };
ngOnInit() {
// Some code
}
}
<mk-progress [minimum]="0" [maximum]="100" [current]="50" class="test">
<span *label>Label</span>
<span *value="let item">{{ item.progress }}%</span>
</mk-progress>
This technique also allows you to provide default content if the templates are not provided:
<ng-container *ngTemplateOutlet="valueTemplate ? valueTemplate : defaultTemplate; context: templateContext">
</ng-container>
<ng-template #defaultTemplate>
default thing to show
</ng-template>
See this article for more details.
progressvariable available within theProgressComponent. Why do you want to project it back into component? If you do want it, you can simply emit the value out and project it back in.