I am trying to select some text in an input field after a component is initialised. I know I can do this with a setTimeout()
, but that feels a bit too hacky.
Most hooks run before the input text is loaded by two-way binding. The others also run every time someone selects the other field.
The code: https://stackblitz.com/edit/angular-pxrssq
import { Component, OnInit, Input, ViewChild } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<input #input type="text" [(ngModel)]="info.one"/>
<input type="text" [(ngModel)]="info.two"/>`,
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Input() info;
@ViewChild('input', {static: true} ) input;
constructor() { }
ngOnInit() {
this.input.nativeElement.select();
}
}
Is there a lifecycle hook that runs once after a components initialisation, but after two-way-binding is loaded?