我是Angular 2中的新用户。我在本地机器中托管了一项服务。该服务工作正常。但是当我要从Angular 2的服务中检索数据时,如下所示:数据成功检索并登录到我的控制台中。但是当我在我看来要显示价值的时候。 “this.myPurchaseItems”是空的。这是为什么?请帮忙。谢谢Angular 2中的服务不会向html组件返回值
calling file......
this._service.getMyPracticeTest(this.uid).subscribe(data => { this.myPurchaseItems = data });
------------------------------------------------------
service.ts file.....
getMyPracticeTest(uid: string){
return this._http.get('http://localhost:49753/RestServiceImpl.svc/getMyPracticeTest/' + uid)
.map(res => res.json())
.do(data => console.log("GetMyPracticeTest" + JSON.stringify(data)))
.catch(this.handleError);
}
------------------------------------------------------
<table class="table">
<tr><th>ID</th></tr>
<tr *ngFor="let data of myPurchaseItems">
<td>{{data.Purchase_ID}}</td>
</tr>
</table>
解决问题的办法。
getMyPracticeTest(uid: string){
return this._http.get('http://localhost:49753/RestServiceImpl.svc/getMyPracticeTest/' + uid)
.map(data => {
data.json();
// the console.log(...) line prevents your code from working
// either remove it or add the line below (return ...)
console.log("I CAN SEE DATA HERE: ", data.json());
return data.json();
}
假设第三个文件是第一个文件的视图,您只是名称与“myPurchaseItems”和“myTestData” – matmo
错字错误不匹配。这是myPurchaseItems。 – wal
如果你的数据是一个数组,那么它理论上应该工作得很好。你有没有把ChangeDetectionStrategy改成OnPush?如果没有,一切都应该按照你的例子工作。很难评论任何进一步没有plunkr。 – matmo