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

我应该再次以角度调用ngoninit()吗?

  •  2
  • vibhor1997a  · 技术社区  · 7 年前

    我刚开始接触棱角分明的人,这个问题听起来可能很愚蠢。请容忍我。

    我已经定义了我的 ngOnInit 比如:

    ngOnInit() {
     this.rowData = this.studentService.getStudents();//Make http calls to populate data
    }
    

    如果需要重新加载数据,我会再次致电Ngoninit:

    onSomeEvent(){
     this.ngOnInit();
    }
    

    这样可以吗? 或者我应该写一行来再次调用HTTP,如果 ngOnInit() 是一种昂贵的方法。

    4 回复  |  直到 7 年前
        1
  •  7
  •   Pardeep Jain    7 年前

    ngOnInit

    ngOnInit() {
     this.onLoad();
    }
    
    onLoad() {
    this.rowData = this.studentService.getStudents();//Make http calls to populate data
    }
    
    onSomeEvent(){
     this.onLoad();
    }
    
        2
  •  2
  •   Akj    7 年前

    ngOnInit(){
    this.loadData();
    }
    
    //load data
    
    loadData(){
    this.rowData = this.studentService.getStudents();
    }
    
    //on change event
    ngOnChanges(){
    this.loadData()
    }
    
    //capture data on other event
    otherEvent(){
    this.loadData()
    }
    
        3
  •  1
  •   Muhammed Albarmavi    7 年前

    ngOnInit

    ngOnInit() {
     this.refrechItems();
    }
    
    public refrechItems(): void {
      // magic things
    } 
    

    推荐文章