重新加载选择更改的数据 - Angular 4

时间:2017-12-01 20:18:59

标签: angular

当我更改我的选择选项时,我正在尝试重新加载我的treetable。我尝试重新加载OnInit方法,但它不起作用。 这是我的组件,即服务_estadoService调用函数,它调用Web服务。

drawerImageLoader

这是我的HTML

constructor(
      private _route: ActivatedRoute,
      private _router: Router,
      private _estadoService: EstadoService
  ) {
    this.titulo = 'Estado de Ingresos y Gastos:';
    this.year = 2017;
  }

  ngOnInit() {
    let TodayDate = new Date();
    this.mes = TodayDate.getMonth();
    this.listaAnios = this.getYears();
    this.listaMonths = this.getMonths();
    this.listaMonthsNames = this.getMonthsNames();

    for (var i = 0; i < this.listaMonths.length; i++) {
      this.finalarr[i] = [this.listaMonths[i],this.listaMonthsNames[i]];
    }

    let sess = localStorage.getItem('datos');
    let datos = sess.split(" ");

    this._estadoService.getEstado(datos[1],this.year).subscribe(
      result => { console.log(result);
        this.children = result.Costos.ItemCosto;
        this.estados = this.getNewData(this.children);
        this.estados = this.getFirstLevel(this.estados, '0');
        this.list_Init = result.Costos.ItemCosto;
        this.obj = this.createTree(null, this.children);
        this.datos= this.obj[0];
      },
      error => {
        console.log(<any>error);
      }
    );
  }

onChange(value) {
    console.log(value);
    this.year = value;
    ngOnInit();
}

我喜欢在我更改选项上的选项时刷新我的treetable。关于如何实现这个目标的任何想法?

1 个答案:

答案 0 :(得分:1)

您永远不应该从组件中的其他方法调用ngOnInit生命周期方法。如果您想要的是在更改选项的选项时再次执行所有逻辑,您可以执行以下操作:

constructor(
  private _route: ActivatedRoute,
  private _router: Router,
  private _estadoService: EstadoService
) {
  this.titulo = 'Estado de Ingresos y Gastos:';
  this.year = 2017;
}

ngOnInit() {
  updateData();
}

updateData() {
  let TodayDate = new Date();
  this.mes = TodayDate.getMonth();
  this.listaAnios = this.getYears();
  this.listaMonths = this.getMonths();
  this.listaMonthsNames = this.getMonthsNames();

  for (var i = 0; i < this.listaMonths.length; i++) {
    this.finalarr[i] = [this.listaMonths[i],this.listaMonthsNames[i]];
  }

  let sess = localStorage.getItem('datos');
  let datos = sess.split(" ");

  this._estadoService.getEstado(datos[1],this.year).subscribe(
    result => { console.log(result);
      this.children = result.Costos.ItemCosto;
      this.estados = this.getNewData(this.children);
      this.estados = this.getFirstLevel(this.estados, '0');
      this.list_Init = result.Costos.ItemCosto;
      this.obj = this.createTree(null, this.children);
      this.datos= this.obj[0];
    },
    error => {
      console.log(<any>error);
    }
  );
}

onChange(value) {
  console.log(value);
  this.year = value;
  updateData();
}