Is there a way of creating #-variables to HTML elements dynamically with *ngFor?
E.G
html-file
<mat-checkbox #{{name}} *ngFor="let name of names" (click)="matBoxClick({{name}})"></mat-checkbox>
ts-file
@ViewChild('checkbox1') private checkbox1:MatCheckbox;
@ViewChild('checkbox2') private checkbox2: MatCheckbox;
@ViewChild('checkbox3') private checkbox3: MatCheckbox;
@ViewChild('checkbox4') private checkbox4: MatCheckbox;
...
names = [
'checkbox1',
'checkbox2',
'checkbox3',
'checkbox4'
];
...
matBoxClick(name) {
console.log(this[name])
}
Whit this code, the console.log prints undifined. If I put the name directly as the variable name, the ts-file is able to find it.
<mat-checkbox #checkbox1 (click)="matBoxClick({{name}})"></mat-checkbox>
The problem seems to come with *ngFor. Any suggestions how I could accomplish this? Thanks for help.