Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

fix(): fix sidenav scroll subscription #284

Merged
merged 1 commit into from
Oct 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/app/shared/table-of-contents/table-of-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ export class TableOfContents implements OnInit {
}

ngOnInit(): void {
this._scrollContainer = this.container ?
this._document.querySelectorAll(this.container)[0] : window;

this._scrollSubscription = Observable
.fromEvent(this._scrollContainer, 'scroll')
.debounceTime(10)
.subscribe(() => this.onScroll());
// On init, the sidenav content element doesn't yet exist, so it's not possible
// to subscribe to its scroll event until next tick (when it does exist).
Promise.resolve().then(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you do this in one of the other lifecycle hooks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. I can move to ngAfterViewInit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: this appears not to work.

this._scrollContainer = this.container ?
this._document.querySelectorAll(this.container)[0] : window;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use querySelector instead of querySelectorAll(...)[0].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just copy-pasting what was there before into a Promise, but I can update if you want.


this._scrollSubscription = Observable
.fromEvent(this._scrollContainer, 'scroll')
.debounceTime(10)
.subscribe(() => this.onScroll());
});
}

ngOnDestroy(): void {
Expand Down