That code worked at least with angular 2 if not even with angular < 4.3.6.
At the moment gradingKey
object is undefined inside the display or edit template.
It is not undefined in the getTemplate(gradingKey) method.
gradingKey is initialized as class field in the
component
@Input() set gradingKeyModel(gradingKeyModel: GradingKeyModel) {
this.gradingKey = gradingKeyModel.gradingKey;
}
Html
<ng-template [ngTemplateOutlet]="getTemplate(gradingKey)"
[ngTemplateOutletContext ]="{ $implicit: gradingKey }">
</ng-template>
<ng-template #displayTemplate let-gradingKey>
<div>
{{gradingKey}}
</div>
</ng-template>
<ng-template #editTemplate let-gradingKey>
<div>
{{gradingKey}}
</div>
</ng-template>
Why is gradingKey undefined inside the templates suddenly?
Did the way how to access ngOutletContext changed when using ngTemplateOutletContext?
ngTemplateOutletContext: Object | null. A context object to attach to the EmbeddedViewRef . This should be an object, the object's keys will be available for binding by the local template let declarations. Using the key $implicit in the context object will set its value as default.
ngTemplateOutlet
needs to be wrapped in ng-container
. You passed to the ngTemplateOutletContext
$implicit variable (which is good), but try to change it to explicit variable like below:
<ng-container [ngTemplateOutlet]="getTemplate(gradingKey)"
[ngTemplateOutletContext ]="{ gradingKey: gradingKey }">
</ng-container>
<ng-template #displayTemplate let-gradingKey="gradingKey">
<div>
{{gradingKey}}
</div>
</ng-template>
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