【发布时间】:2017-11-03 01:50:39
【问题描述】:
我已经构建了一个 Angular 2 应用程序来从 oracle db 中获取数据,并在视图中显示它,每当我在路由之间切换时,每次加载视图以从数据库中获取数据时。我怎样才能让它只获取一次数据,直到我点击一些控件来获取数据。
服务:
getTaskDetails(taskId : Number): Promise<String[]> {
this.heroesUrl='http://localhost:3007/getTaskDetails';
return this.http.get(this.heroesUrl+"?taskId="+taskId)
.toPromise()
.then(function(response){
console.log("Test"+response.json());
return response.json();
})
.catch(this.handleError);
}
组件:
getHeroes(agentID : Number,filterDateFrom : String,filterDateTo : String): void {
this.heroService
.getTaskByDate(taskId)
.then((dataGSD)=>
this.dataGdsp = dataGSD
);
查看:
<table *ngIf="dataGdsp" id="customers">
<tr>
<th>AGENT_ID</th>
<th>TASK_ID</th>
<th>PARENT_PROCESS_NAME</th>
<th>INITIATION_POINT_NAME</th>
</tr>
<tr *ngFor="let d of dataGdsp; let i = index">
<td>{{d.AGENT_ID }}</td>
<td>{{d.BPM_INSTANCE_ID}}</td>
<td>{{d.PARENT_PROCESS_NAME}}</td>
<td>{{d.INITIATION_POINT_NAME}}</td>
</tr>
</table>
【问题讨论】:
-
你在哪里调用
getHeroes的部分? -
首先想到的是“使用服务”,但您的问题包括一个。在@RichardMatsen 可能重复之后,我不熟悉问题空间,但如果您试图在路由之间保留组件,线程会导致
RouteReuseStrategy:angular.io/api/router/RouteReuseStrategy -
RouteReuseStrategy 对我有用。非常感谢
标签: angular