【问题标题】:Navigating to other component and back does not trigger ngFor again导航到其他组件并返回不会再次触发 ngFor
【发布时间】:2019-11-03 09:53:29
【问题描述】:

我有一个 Angular 应用程序,其中包含两个可以通过导航栏导航到的组件。两个组件都代表自己的视图,并定期向 API 发出 http 请求。

当我在组件之间来回导航时,一个组件的表中的ngFor 不再触发。它仅在我重新加载页面或第一次导航到那里时才有效。即使更改基础数据也不起作用。

如何在组件初始化后再次触发ngFor 并导航回它?我只实例化这个组件一次,所以我知道这是同一个视图。

编辑:这是文件。

state.components.ts:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpRequestService } from '../http-request.service';


@Component({
  selector: 'app-state',
  templateUrl: './state.component.html',
  styleUrls: ['./state.component.css']
})
export class StateComponent implements OnInit, OnDestroy {


constructor(private requestService: HttpRequestService) {

}

ngOnInit() {
  if (!active) {
    active = true;
    setInterval(() => this.httprequest(), 1000);
}

private httprequest() {
// this part changes the underlying data
this._getState('http://localhost:8090/agvs/status/short')
  .subscribe(info => {
    this._agvList = []; // implement in the empty list
    let tmpTime = 0;
    info.forEach(element => {
      if (this._agvList.find(x => x.Id === element.Id) !== undefined) {
        tmpTime = this._agvList.find(x => x.Id === element.Id).Error_time;
      }
      const tmpAgv: Agv_stat = {
        Id: element.Id,
        Online: element.Online,
        Volt: element.Volt,
        Soc: element.Soc,
        Pos: {
          Map: element.Pos.Map,
          X: element.Pos.X,
          Y: element.Pos.Y,
          T: element.Pos.T
        },
        Error: element.Error,
        Old: element.old, // intern
        Error_time: tmpTime // intern
      }
      this._agvList.push(tmpAgv);

      // console.log( 'surch robot ' + this._agvListOld.find(x => x.Id === tmpAgv.Id) );
      if (this._agvListOld.find(x => x.Id === tmpAgv.Id) == undefined) {
        console.log('list_old' + JSON.stringify(this._agvListOld));
        console.log('save robot ' + tmpAgv.Id);
        this._agvListOld.push(tmpAgv);
        console.log('new_list:' + JSON.stringify(this._agvListOld));
      } else {
        tmpAgv.Error_time = this._agvListOld.find(x => x.Id === tmpAgv.Id).Error_time;
      }

      if (tmpAgv.Error !== this._agvListOld.find(x => x.Id === tmpAgv.Id).Error || tmpAgv.Error === '') {
        // console.log(" reset timer !!!");
        tmpAgv.Old = tmpAgv.Error;
        tmpAgv.Error_time = 0;
        this._agvListOld.find(x => x.Id === tmpAgv.Id).Error_time = 0;
      } else {
        // console.log("timer +1");
        this._agvListOld.find(x => x.Id === tmpAgv.Id).Error_time += 1;
        tmpAgv.Error_time = this._agvListOld.find(x => x.Id === tmpAgv.Id).Error_time;
      }
      if (tmpAgv.Error_time > 30) { // after 0.5 minute
        console.log('Add an error : ' + tmpAgv.Error);
        const tmp_err: ErrorState = {
          Id: tmpAgv.Id,
          second: tmpAgv.Error_time,
          description: tmpAgv.Error,
        }
        if (this._agvErrorList.find(x => x.Id === tmpAgv.Id) == undefined) {
          console.log('new Error is created !!!');
          this._agvErrorList.push(tmp_err); // create a new error list
        } else {
          // update the message sec and error text
          this._agvErrorList.find(x => x.Id === tmpAgv.Id).second = tmpAgv.Error_time;
          this._agvErrorList.find(x => x.Id === tmpAgv.Id).description = tmpAgv.Error;
          console.log('timer list :' + tmpAgv.Error_time);
          console.log(this._agvErrorList);
        }
      } else {
        // TODO: delete the error if the timer is reset
      }
      // console.log('Mapped new element: ' + JSON.stringify(tmpAgv)); // stringify(): change json to string
    });
    console.log('agvlist :' + JSON.stringify(this._agvList));
  }, error => {
    console.error(JSON.stringify(error));
    this._agvList = [];
  });
  }
}

state.component.html

<table id="customers">
  <tr>
  <th>Robot</th>
  <th>Batterie</th>
  <th>Zustand</th>
  </tr>
<tr *ngFor="let Agv_stat of _agvList">
  <td>{{Agv_stat.Id}}</td>
  <td>{{Agv_stat.Soc}}</td>
  <td>{{Agv_stat.Error}}</td>
</tr>    
</table>

【问题讨论】:

  • 你能分享那段代码吗?
  • 您能否发布 .ts 文件的内容。将有助于回答。
  • 如何在组件之间导航?

标签: angular typescript ngfor


【解决方案1】:

我不确定,但也许这会有所帮助,当您推送到数组时不会发生更改检测。因此 ngFor 不会再次触发。解决这个问题的方法是使用扩展运算符或 Object.assign 不可变地更新 _agvList。

【讨论】:

  • 不知道反对票从何而来,不是我。我会尝试你的建议,但我怀疑它是否有效,因为我已经清空并重新填充了底层数组。
  • 成功了吗?制作一个数组 = [ ] 将触发更改检测,但不会将内容推入其中,因此我想为什么 ngFor 没有运行。您可以尝试添加一个临时数组,在其中推送元素,然后执行 array = tempArray
猜你喜欢
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2012-09-24
  • 1970-01-01
相关资源
最近更新 更多