My ngAfterViewInit function is not working as expected. I have feeling that I am missing something here.
ngOnInit() {
this.dataService.getUsers().subscribe((users) => {this.users = users) ;
}
ngAfterViewInit() {
if (document.getElementById('target')) {
document.getElementById('target').scrollIntoView({
behavior: 'auto',
block: 'center',
inline: 'center',
});
}
So basically, I'm using ngAfterViewInint()
function to scroll to the current item, after the view complete loaded. Inside ngOnInit()
, if I am not calling subscribe()
and just put a variable there. It works just fine. But its not the case that what i am asking.
I have a work around by using setTimeOut()
inside ngAfterViewInit()
. But I don't like it. I dont like the fact that its relies on timeout. And I think there would be a better solution for it.
Any ideal ? Thanks all.
My ngAfterViewInit function is not working as expected. I have feeling that I am missing something here.
ngOnInit() {
this.dataService.getUsers().subscribe((users) => {this.users = users) ;
}
ngAfterViewInit() {
if (document.getElementById('target')) {
document.getElementById('target').scrollIntoView({
behavior: 'auto',
block: 'center',
inline: 'center',
});
}
So basically, I'm using ngAfterViewInint()
function to scroll to the current item, after the view complete loaded. Inside ngOnInit()
, if I am not calling subscribe()
and just put a variable there. It works just fine. But its not the case that what i am asking.
I have a work around by using setTimeOut()
inside ngAfterViewInit()
. But I don't like it. I dont like the fact that its relies on timeout. And I think there would be a better solution for it.
Any ideal ? Thanks all.
target
element depend on data returning from your request? – Pytth Commented Dec 17, 2019 at 22:25