JonnyBGod / ngx-scrollspy

Angular ScrollSpy Service

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not working: Cannot read property 'subscribe' of undefined

MurhafSousli opened this issue · comments

Hi, I tried to add ng2-scrollspy to angular2 webpack starter project. I'm getting this error ORIGINAL EXCEPTION: TypeError: Cannot read property 'subscribe' of undefined

import {ScrollSpyDirective, ScrollSpyService} from 'ng2-scrollspy';

@Component({
  selector: 'single',
  template: require('./single.html'),
  directives: [ScrollSpyDirective]
})
export class Single{
  constructor(public scrollSpyService: ScrollSpyService){
  }
  ngAfterViewInit(){
    this.scrollSpyService.getObservable('test').subscribe((e: any) => {
      console.log('ScrollSpy::test: ', e);
    });
  }
}

template

<div class="post-card">
  <div scrollSpyElement="test" class="post-header fillheight">
    <div class="post-image" *ngIf="post.featuredMedia()"
        [ngStyle]="getFeaturedImage()">
    </div>
...

You should use ScrollSpyElementDirective instead when you want to track scroll on an element and not "window".

import {Component, Injectable, AfterViewInit} from '@angular/core';
import {ScrollSpyElementDirective, ScrollSpyService} from 'ng2-scrollspy';

@Injectable()
@Component({
    selector: 'yourComponent',
    template: `
    <div scrollSpyElement="test" style="max-height: 100px; overflow: auto;">
        <div style="height: 500px;"></div>
    </div>`,
    directives: [ScrollSpyElementDirective]
})
export class YourComponent implements 
 {
    constructor(scrollSpyService: ScrollSpyService) {
        this.scrollSpyService = scrollSpyService;
    }

    ngAfterViewInit() {
        this.scrollSpyService.getObservable('test').subscribe((e: any) => {
            console.log('ScrollSpy::test: ', e);
        });
    }
}

Now it works, thank you

Hi, guys. I have everything setup up correctly and I am getting no console.log .
I'm using rc4. Literally, no errors now. The replaySubject from getObservable does not emit anything....

Hi, guys. I have everything setup up correctly and I am getting no console.log .
I'm using rc4. Literally, no errors now. The replaySubject from getObservable does not emit anything....

Same here!

i am unable to test the component i am getting error that follows in spec.ts file
TypeError: Cannot read property 'subscribe' of undefined