Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngTemplateOutletContext context source is undefined

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?

like image 678
HelloWorld Avatar asked Jan 10 '18 20:01

HelloWorld


People also ask

What is 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.


1 Answers

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>
like image 101
ssuperczynski Avatar answered Oct 15 '22 10:10

ssuperczynski