Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScrollSpyService throws item with the id [...] already exists when routing #46

Open
ElyesaErgezen opened this issue May 30, 2022 · 0 comments

Comments

@ElyesaErgezen
Copy link

I am getting an error (as below) when I route to an another page and route back.
E.x. Routing from home page to contact page and returning back to home page

Any help is appreciated!

Version list

  • Angular v10.2.5
  • @uniprank/ngx-scrollspy: ^1.1.0

ERROR

image

app.module.ts

import { NgxScrollspyModule } from '@uniprank/ngx-scrollspy';

imports: [
// other imports
...
NgxScrollspyModule.forRoot()
]

home.component.html

Mobile horizontal navbar

<ul #mobileScroll>
    <li *ngIf="favCat?.length>0" (click)="func.scrollTo('c-0')" uniScrollItem="c-0" id="c-0-m">
      ...
    </li>
    <li *ngFor="let cat of categories" uniScrollItem="c-{{cat.id}}" id="c-{{cat.id}}-m (click)="func.scrollTo('c-' + cat.id)">
      ...
    </li>
</ul>

Desktop column for categories

<ul #navbarCategory>
  <li (click)="func.scrollTo('c-0')" *ngIf="favCat?.length>0" uniScrollItem="c-0" id="c-0-d">
    ...
  </li>
  <li *ngFor="let cat of categories" (click)="func.scrollTo('c-' + cat.id)" uniScrollItem="c-{{cat.id}}" id="c-{{cat.id}}-d">
    ...
  </li>
</ul>

Categories section

<section *ngIf="favCat?.length>0" uniScrollSpy="c-0">
    ...
</section>

<section *ngFor="let cat of categories" uniScrollSpy="c-{{cat.catId}}">
    ...
</section>

home.component.ts

import { ScrollSpyService, ScrollObjectInterface } from '@uniprank/ngx-scrollspy';

export class HomeComponent implements OnInit, AfterViewInit {
  private _subscription: Subscription;
  
  constructor(private _scrollSpyService: ScrollSpyService) { }
  
  ngAfterViewInit(): void {
    this._scrollSpyService.setOffset('window', 60);
    this._subscription = this._scrollSpyService.observe('window').subscribe((element: ScrollObjectInterface) => {
      if(element != null) {
        if(!this.func.preventScrollDetection) {
          var col = document.getElementById(element.id + '-m');
          if (col != null && this.mobileScroll.nativeElement != null) {
            this.mobileScroll.nativeElement.scrollLeft = (col.offsetLeft - (this.mobileScroll.nativeElement.offsetWidth / 2)) + (col.offsetWidth / 2);
          }
        }
      }
    });
  }
  
  ngOnDestroy() {
    if (this._subscription) {
      this._subscription.unsubscribe();
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant