It seems to me that I am missing something here, and I would like to know if there is a better way to do this.
I have a directive that basically adds a class to the parent of an input field. When loading, I need to check if the input field has a value, you would think that it will be simple ...
Now I have used several different options that include ngOnInit, AfterContentInitand AfterViewInit. But for below, I'm still on AfterContentInit:
ngAfterContentInit() {
console.log(this.el.nativeElement);
console.log(this.el.nativeElement.value);
setTimeout(() => {
console.log(this.el.nativeElement.value);
}, 1000);
this.activeStateCheck(this.el.nativeElement);
}
activeStateCheck(element) {
if (element.value !== '') {
element.parentElement.classList.add('active');
return true;
}
}
I must indicate that all parameters give the same results.
The first console prints an empty string, and the second prints the actual string. In this case, this is a "Grand Tour".
My question is: how do I do this without using setTimeout?
, , . , OnInit ?
, , , , , .
. appPlaceholder:
<fieldset class="form__placeholder">
<input type="text"
id="lastName"
formControlName="lastName"
appPlaceholder>
<label for="lastName"
class="form__placeholder--label">
Lastname:
</label>
</fieldset>