代码之家  ›  专栏  ›  技术社区  ›  user944513

为什么不使用角度推入数组中的项?

  •  0
  • user944513  · 技术社区  · 8 年前

    当用户滚动或触底时,我试图在列表中添加更多项。我发出指令并检查滚动位置,如果用户到达底部,我使用 event emitter .调用了我的函数,但它未在数组中添加项。

    这是我的代码:

    https://stackblitz.com/edit/angular-zsedct?file=src%2Fapp%2Fscroll.directive.ts

    scroll = (e): void => {
    
    
        //e.target.scrollTop = current scroll position
        //e.target.scrollHeight - e.target.offsetHeight - to calculate the bottom. equivalent to scrollTopMax in FF
        if (e.target.scrollTop == e.target.scrollHeight - e.target.offsetHeight) {
          console.log('You reached the bottom!');
          this.valueChange.next();
        }
    
        //handle your scroll here
        //notice the 'odd' function assignment to a class field
        //this is used to be able to remove the event listener
      }; 
    

    成分

    addMoreElement(){
        console.log('add more button')
         for(var i=0;i<5;i++){
          this.arr.push('hellp'+i)
        }
      }
    

    有没有更好的方法来完成同样的任务?你能告诉我另一种方法来实现同样的事情,并做一些改进吗

    1 回复  |  直到 8 年前
        1
  •  2
  •   Sajeetharan    8 年前

    为了在模板上反映数据更改,您需要使用detect changes,

     constructor(private cdr:ChangeDetectorRef) {
    

      this.cdr.detectChanges();
    

    STACKBLITZ DEMO

    推荐文章