目前我正在学习Angular。大多数事情有些不起作用。为了了解更多,我有疑问。在我的组件中,我从一个名为json的函数中获得了回报。 这是我的代码:
total Items: number = 3; //<-- data.total_items needed here
ngOnInit() {
return this.customerdataService.getCustomersList().subscribe(data => {
console.log(data)
});
}
到目前为止,数据显示在组件输出中。但是对于分页,我需要在CustomerComponent类中包含数据。在这里,我必须定义totalItems,currentPage等。我尝试了不同的方法,但没有任何结果,例如:data.current_page 我必须在构造函数中使用它吗?
答案 0 :(得分:1)
您的代码意义不大,ngOnInit
不会返回任何内容,也不会有任何帮助,因为您也无法对订阅执行太多操作。
可观察对象发出数据,并且该数据将在subscribe
的数据回调期间可用。
this.customerdataService.getCustomersList().subscribe(
data => {
console.log(data);
this.total_Items = data.total_items; // assignment
});