помните, что вам нужно выдохнуть Angular, чтобы убедиться, что он виден или нет. Вы даете это дыхание, используя setTimeout ()
Представьте, что у вас есть форма типа
<form [formGroup]="form">
<input type="checkbox" [ngModel]="check"
(ngModelChange)="check=$event;focus()"
[ngModelOptions]="{standalone: true}"
>Surname visible
<input formControlName="name">
<input #surnameControl *ngIf="check" formControlName="surname">
</form>
В вашем .ts
@ViewChild('surnameControl',{static:false}) control:ElementRef
focus()
{
//outside setTimeout this.control is undefined
setTimeout(()=>{
this.control.nativeElement.focus()
})
}
См. простой пример