【发布时间】:2018-08-08 03:25:01
【问题描述】:
我正在处理一个场景,我必须使用 rest 调用从 springboot 检索对象列表,并使用 angular 5 将它们填充到 UI 中。
我的组件看起来像这样,
export class BusinessComponent implements OnInit {
serviceSavings:Services[] = [];
constructor(private ServicesService:ServicesService) {
}
ngOnInit() {
this.ServicesService.getServicesByCatID(PRODUCTSAVINGSID)
.subscribe(serviceSavings => this.serviceSavings = serviceSavings);
console.log("The length of serviceSavings :", this.serviceSavings.length); //this always prints zero.
}
examplemethod() {
console.log("The length of serviceSavings :", this.serviceSavings.length);
}
}
现在的问题是 init() 方法中的日志语句打印了 0 行服务。我需要从列表中检索每个服务并对其执行一些操作。
当我放置在用户定义的方法中时相同的日志,例如在 UI 中使用用户操作调用它。它根据数据库表显示列表的确切计数。
我需要在应用程序加载期间在 init() 中编写一些业务逻辑,以在 UI 中显示某些值。
但在应用程序加载时,在 init() 中检索到的列表的长度始终为 0。我无法遍历值。我认为数组对象存在一些更新问题。
我的服务方法是这样的,
getServicesByCatID(categoryid){
return this.http.get(this.getServiceUrlByCategory(categoryid))
.map(res => res.json());
}
springboot 应用程序完美返回对象列表,从后端没有问题。
有人可以帮我找出问题吗?
【问题讨论】:
-
PRODUCTSAVINGSID是什么?如果您在 chrome 控制台中查看network或其他内容,您实际上能看到任何结果吗? -
你应该把你的 console.log 放在订阅的返回中
-
productSavingsID 是一个 int 值,我在这里没有提到,但我在代码中提到了。是的,我在 chrome 中检查了日志语句正在打印,但它在 init 日志中始终为 0
-
@HMarteau : 那是我做的一样..
标签: angular rest typescript http