Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind to 'ngTemplateOutletContext'

Tags:

angular

I have converted Angular 4 to Angular 6 and I have built the project and geting the following error. Any idea how to handle?

Can't bind to 'ngTemplateOutletContext' since it isn't a known property of 'ng-container'.

<ul *ngIf="item.childrens != undefined && item.childrens.length > 0">
   <ng-container *ngTemplateOutletContext="recursiveList; context:{ $implicit: item.childrens }"></ng-container>
</ul>

My question is on TemplateOutletContext, not TemplateOutlet.

like image 344
casillas Avatar asked Jul 09 '18 15:07

casillas


People also ask

Can't bind to ngTemplateOutlet since it isn't a known property of?

This likely means that the library (@angular/common) which declares NgTemplateOutlet has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so.

How to use ngTemplateOutlet in Angular?

ngTemplateOutlet is a structural directive. We use it to insert a template (created by ngTemplate ) in various sections of our DOM. For example, you can define a few templates to display an item and use them display at several places in the View and also swap that template as per the user's choice.

What is context in ngTemplateOutlet?

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. @Input() ngTemplateOutlet: TemplateRef<any> | null.

What is $implicit in Angular?

You can define local variables on ng-template through let-name. When angular creates a template by calling createEmbeddedView it can also pass context that will be used inside ng-template. Using the key $implicit in the context object will set its value as default.


1 Answers

As per the docs:

ngTemplateOutletContext bound to NgTemplateOutlet.ngTemplateOutletContext

So you would be able to attach context in the following way:

<ng-container 
  [ngTemplateOutlet]="recursiveList"
  [ngTemplateOutletContext]="{ $implicit: item.childrens }"></ng-container>

Or:

<ng-container *ngTemplateOutlet="recursiveList; context: { $implicit: item.childrens }"></ng-container>

Angular needs to know what view you intend on adding your context object to, so ngTemplateOutletContext won't work on its own AFAIK.

Source, Demo

like image 102
Michael Doye Avatar answered Nov 09 '22 08:11

Michael Doye