After migrating Angular 6.x to Angular 12.x, I’m facing template related issues.
UPDATED
I’ve my code like this
JavaScript
x
15
15
1
<table>
2
<thead></thead>
3
<tr>
4
<th>
5
<input #selectAll type="checkbox" [checked]="myFunctionName()" (click)="toggleSelection()"
6
/>
7
</th>
8
<th>
9
<span *ngIf="myFunctionName(); then removeAll ; else selectAll"></span>
10
<ng-template #selectAll>{{SELECT_ALL}}</ng-template>
11
<ng-template #removeAll>{{DESELECT_ALL}}</ng-template>
12
</th>
13
</tr>
14
</table>
15
Below error i got
ERROR Error: ngIfElse must be a TemplateRef, but received ‘[object HTMLInputElement]’
What i tried?
JavaScript
1
15
15
1
<table>
2
<thead></thead>
3
<tr>
4
<th>
5
<input #selectAll type="checkbox" [checked]="myFunctionName()" (click)="toggleSelection()"
6
/>
7
</th>
8
<th>
9
<ng-container *ngIf="myFunctionName(); then removeAll ; else selectAll"></ng-container>
10
<ng-template #selectAll>{{SELECT_ALL}}</ng-template>
11
<ng-template #removeAll>{{DESELECT_ALL}}</ng-template>
12
</th>
13
</tr>
14
</table>
15
ts
JavaScript
1
8
1
@ViewChild('selectAll' , {static: true}) selectAll!: ElementRef<HTMLInputElement>;
2
myFunctionName(): boolean {
3
if (this.checkboxes) {
4
// other stufs
5
}
6
return false;
7
}
8
Could someone help me how to can refactor pieces ?
Thanks for the help guys
Advertisement
Answer
so the problem is very simple, you have #selectAll twice in your HTML code. to fix this you need to rename one of them
JavaScript
1
4
1
<ng-container *ngIf="myFunctionName(); then removeAllBlock; else selectAllBlock"></ng-container>
2
<ng-template #selectAllBlock>{{SELECT_ALL}}</ng-template>
3
<ng-template #removeAllBlock>{{DESELECT_ALL}}</ng-template>
4
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)
JavaScript
1
2
1
<input #myInput type="checkbox" [checked]="myFunctionName()" (click)="toggleSelection(myInput)"/>
2
JavaScript
1
4
1
toggleSelection(myInput:HTMLInputElement){
2
// do stuff with your input
3
}
4