Behebung von Angular 'TypeError: templateRef.createEmbeddedView is not a function'
English
Deutsch
Problem:
Du triffst auf eine Fehlermeldung wie diese:
error_trace.txt
ERROR TypeError: templateRef.createEmbeddedView is not a function
at ViewContainerRef_.createEmbeddedView (core.js:11389)
at NgIf._updateView (common.js:2843)
at NgIf.set [as ngIfElse] (common.js:2815)
at updateProp (core.js:12602)
at checkAndUpdateDirectiveInline (core.js:12313)
at checkAndUpdateNodeInline (core.js:13876)
at checkAndUpdateNode (core.js:13819)
at debugCheckAndUpdateNode (core.js:14712)
at debugCheckDirectivesFn (core.js:14653)
at Object.eval [as updateDirectives] (MyComponent.html:1)in einer Komponente, deren Quellcode in etwa so aussieht
ngIf_else_broken.html
<div *ngIf="myCondition ; else elseSection">
<!-- ... -->
</div>
<div #elseSection>
<!-- ... -->
</div>Lösung
Welches Element auch immer du in der *ngIf-else-Klausel referenzierst, es kann keine beliebige Komponente sein, sondern muss ein ng-template sein.
Um dies zu beheben, ändere <div #elseSection> zu <ng-template #elseSection>. Beachte, dass die Verwendung von nur <template> seit Angular4 veraltet ist.
Der resultierende Quellcode sollte so aussehen:
ngIf_else_fixed.html
<div *ngIf="myCondition ; else elseSection">
<!-- ... -->
</div>
<ng-template #elseSection>
<!-- ... -->
</ng-template>Check out similar posts by category:
Angular, Javascript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow