【问题标题】:How to bind data to html in angular 2/4 by using thread or async如何使用线程或异步以 2/4 角度将数据绑定到 html
【发布时间】:2018-03-14 18:03:18
【问题描述】:

我有一个组件,它有子组件。在 ngOnInit() 我正在调用 Web API 并获取数据列表。

列表的初始点长度是10,但它会更多。

需要在后台执行一些方法(task|process|job)以在一个循环中以 10 x 10 的形式获取其余数据,无论用户当前在做什么,他都在执行哪个组件,这将与后台的其他任务并行运行/她正在与之互动。并执行该方法,使其不会阻塞其他人。

这样做的正确方法是什么?

【问题讨论】:

标签: multithreading angular


【解决方案1】:

对我来说似乎是一个递归调用!

firstResults: any[] = []; // First 10 results to show to your user
results: any[] = []; // All results
currentPosition = 0; // The current position of your last result fetched

getData() {
  this.myService.getResults().subscribe(results => {
    if(!this.firstResults.length) {
      this.firstResults = results
    }
    this.results.push(...results);
    this.currentPosition += results.length;
    this.getData();
  });
}

【讨论】:

  • 感谢 trichetriche,递归没有问题,我需要在线程上获取数据。没有块用户与应用程序交互。
【解决方案2】:

我不认为你正在尝试这样做。但如果列表不是那么大,您可以获取所有数据并对数组“分页”

allData:any[];
page:number=0; //page is 0,1,2,3,4....
paginateData:any[]
this.httpClient.get("url").subscribe(res=>{
     allData=res;
     paginateData=allData.slice(10*this.page,10*(this.page+1));
}

【讨论】:

  • 感谢 Eliseo,我的问题是使用线程,因为列表可能包含 1000 个数据。这里我以 10 x 10 为例。视情况而定。
猜你喜欢
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 2021-10-04
  • 2015-12-08
  • 2018-11-16
  • 2023-03-06
相关资源
最近更新 更多