如何在Angular 4

时间:2017-07-05 07:40:54

标签: angular typescript

当我尝试在文档外调用函数时,就可以正常工作但不在内部工作。

ngOnInit() {
      this.addItems(0, 2);
      $(document).ready(() => {
         if($('html').hasClass('fp-enabled')){
                $.fn.fullpage.destroy('all');
            }
        $('#fullpage').fullpage({ 
            onLeave: (index, nextIndex, direction) => {
                this.no_of_post = 5;
                if(direction == 'down' && index+1 >= this.no_of_post){
                    const start = this.no_of_post;
                    this.no_of_post += 1;
                    this.addItems(start, index+2);
                    console.log(index+1);
                }
            }
        });
      });
  }

addItems(startIndex, endIndex) {
    for (let i = startIndex; i < endIndex; ++i) {
        this.newService.fetchData().subscribe(data=>{
           this.single_post = data[i];
           this.posts.push(this.single_post);
        }); 
    }
  }

以上代码有效,但当我在this.addItems(0, 2);内移动$(document).ready(() => {时,它无效。

1 个答案:

答案 0 :(得分:0)

试试这个

constructor(
    private zone: NgZone
) {}

ngOnInit() {
    this.zone.run(() => {
        // Your code here, stub for the sake of the example
        document.ready = () => {
            console.log('Code ran when document is ready, and stays in the scope of Angular.');
        };
    });
}