How can you focus on element inside ngIf?
This ngIf is true when the page loads but it still gets an undefined error:
@ViewChild("currentDaySpentInput") currentDaySpentInput: ElementRef;
// ...
<div class="spending-input" *ngIf="!spentInputComplete">
<label>How much did you spend today?</label>
<input [(ngModel)]="currentDay.spent" (keyup)="onKey($event)" #currentDaySpentInput>
</div>
focus element
ngOnOnit() {
this.currentDaySpentInput.nativeElement.focus();
}
Error:
ERROR TypeError: Cannot read property 'nativeElement' of undefined
I tried using the setter suggested in this question but it didn't work.