[英]Angular 12 - *ngIf;Then;Else - must be a TemplateRef, but received '[object HTMLInputElement]'
After migrating Angular 6.x to Angular 12.x, I'm facing template related issues.将 Angular 6.x 迁移到 Angular 12.x 后,我遇到了与模板相关的问题。
UPDATED更新
I've my code like this我有这样的代码
<table>
<thead></thead>
<tr>
<th>
<input #selectAll type="checkbox" [checked]="myFunctionName()" (click)="toggleSelection()"
/>
</th>
<th>
<span *ngIf="myFunctionName(); then removeAll ; else selectAll"></span>
<ng-template #selectAll>{{SELECT_ALL}}</ng-template>
<ng-template #removeAll>{{DESELECT_ALL}}</ng-template>
</th>
</tr>
</table>
Below error i got我得到以下错误
ERROR Error: ngIfElse must be a TemplateRef, but received '[object HTMLInputElement]'
ERROR 错误:ngIfElse 必须是 TemplateRef,但收到了“[object HTMLInputElement]”
What i tried?我试过什么?
<table>
<thead></thead>
<tr>
<th>
<input #selectAll type="checkbox" [checked]="myFunctionName()" (click)="toggleSelection()"
/>
</th>
<th>
<ng-container *ngIf="myFunctionName(); then removeAll ; else selectAll"></ng-container>
<ng-template #selectAll>{{SELECT_ALL}}</ng-template>
<ng-template #removeAll>{{DESELECT_ALL}}</ng-template>
</th>
</tr>
</table>
ts ts
@ViewChild('selectAll' , {static: true}) selectAll!: ElementRef<HTMLInputElement>;
myFunctionName(): boolean {
if (this.checkboxes) {
// other stufs
}
return false;
}
Could someone help me how to can refactor pieces?有人可以帮我如何重构片段吗?
Thanks for the help guys谢谢你们的帮助
This should fix your error:这应该可以解决您的错误:
<table>
<thead></thead>
<tr>
<th>
<ng-container *ngIf="myFunctionName(); else selectAll">
{{someEslePartWillComeHere}}
</ng-container>
<ng-template #selectAll>{{someThenableContentGoesHere}}</ng-template>
</th>
</tr>
</table>
The ngIf
directive should wrapper the content that you want to show when condition is true
. ngIf
指令应该包装您想要在条件为true
时显示的内容。
Your code is fine, except the template reference variable used in ng-template.您的代码很好,除了 ng-template 中使用的模板引用变量。 For example: there should not be any properties in your class as in the same name as #selectAll and #removeAll references
例如:在您的 class 中不应有与 #selectAll 和 #removeAll 引用同名的任何属性
<ng-template #selectAll>{{someThenableContentGoesHere}}</ng-template>
<ng-template #removeAll>{{someEslePartWillComeHere}}</ng-template>
so the problem is very simple, you have #selectAll twice in your HTML code.所以问题很简单,你的 HTML 代码中有两次#selectAll。 to fix this you need to rename one of them
要解决此问题,您需要重命名其中一个
<ng-container *ngIf="myFunctionName(); then removeAllBlock; else selectAllBlock"></ng-container>
<ng-template #selectAllBlock>{{SELECT_ALL}}</ng-template>
<ng-template #removeAllBlock>{{DESELECT_ALL}}</ng-template>
and if you use the selectAll
input only in your function, you don't need the @ViewChild but you can do it like this: (just an improvement, not mandatory)如果您仅在 function 中使用
selectAll
输入,则不需要 @ViewChild 但您可以这样做:(只是一种改进,不是强制性的)
<input #myInput type="checkbox" [checked]="myFunctionName()" (click)="toggleSelection(myInput)"/>
toggleSelection(myInput:HTMLInputElement){
// do stuff with your input
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.