admin管理员组文章数量:1426788
I want to scoll to a div after my view has initialized the first time. My problem so far is that AfterViewInit leads to a problem, because its not fully rendered. (Fetched data from server, then render that..)
Code so far:
scrollTo(): void {
if (!this.jumpedToMessage && this.jumpMessage != "0") {
const element = document.getElementById(this.jumpMessage);
// @ts-ignore
element.scrollIntoView({behavior: 'auto', block: 'center', inline: 'nearest'});
this.jumpedToMessage = true
}
}
Error code in console:
my-ponent.ts:58 ERROR TypeError: Cannot read properties of null (reading 'scrollIntoView') at my-ponent.scrollTo (my-ponentponent.ts:76:15) at Object.next (my-ponentponent.ts:65:18) at ConsumerObserver.next (Subscriber.js:91:33) at SafeSubscriber._next (Subscriber.js:60:26) at SafeSubscriber.next (Subscriber.js:31:18) at map.js:7:24 at OperatorSubscriber._next (OperatorSubscriber.js:13:21) at OperatorSubscriber.next (Subscriber.js:31:18) at filter.js:6:128 at OperatorSubscriber._next (OperatorSubscriber.js:13:21)
Any ideas how to solve this?
Important: Initialize only once after the first rendering
I want to scoll to a div after my view has initialized the first time. My problem so far is that AfterViewInit leads to a problem, because its not fully rendered. (Fetched data from server, then render that..)
Code so far:
scrollTo(): void {
if (!this.jumpedToMessage && this.jumpMessage != "0") {
const element = document.getElementById(this.jumpMessage);
// @ts-ignore
element.scrollIntoView({behavior: 'auto', block: 'center', inline: 'nearest'});
this.jumpedToMessage = true
}
}
Error code in console:
my-ponent.ts:58 ERROR TypeError: Cannot read properties of null (reading 'scrollIntoView') at my-ponent.scrollTo (my-ponent.ponent.ts:76:15) at Object.next (my-ponent.ponent.ts:65:18) at ConsumerObserver.next (Subscriber.js:91:33) at SafeSubscriber._next (Subscriber.js:60:26) at SafeSubscriber.next (Subscriber.js:31:18) at map.js:7:24 at OperatorSubscriber._next (OperatorSubscriber.js:13:21) at OperatorSubscriber.next (Subscriber.js:31:18) at filter.js:6:128 at OperatorSubscriber._next (OperatorSubscriber.js:13:21)
Any ideas how to solve this?
Important: Initialize only once after the first rendering
Share Improve this question asked Nov 26, 2022 at 17:29 TimoTimo 681 silver badge7 bronze badges1 Answer
Reset to default 5Maybe you can try a different approach to achieve your goal, by using @ViewChild
, which would be more Angular-like anyway.
In your HTML:
<p #scrollElement>Scroll here</p>
Then in your TS-File:
@ViewChild("scrollElement")
scrollEl: ElementRef;
ngAfterViewInit() {
this.scrollEl.nativeElement.scrollIntoView({ behavior: "smooth", block: "start" });
}
本文标签: javascriptAngular 14Scroll to div with specific idStack Overflow
版权声明:本文标题:javascript - Angular 14 - Scroll to div with specific id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745395548a2656797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论