【发布时间】:2020-06-08 09:20:01
【问题描述】:
我有一项服务向服务器发出 GET 请求以获取值。 当我要将值存储在组件中的变量中时出现问题。
abc.service.ts
public data: Category[] = [];
getPosts():Observable<Category[]> {
return this.http.get<Category[]>(this.url + '/getAllExamCategory');
}
def.component.ts
ELEMENT_DATA: Category[];
dataSource: any;
columnsToDisplay = ['id', 'examCategoryName', 'isActive', 'Action'];
constructor(private adminCategory: AdminCategoryService) { }
ngOnInit() {
this.adminCategory.getPosts().subscribe((reponse: Category[]) => {
if(!reponse) {
return;
}
this.ELEMENT_DATA = reponse;
this.dataSource = new MatTableDataSource<Category>(this.ELEMENT_DATA);
this.dataSource.paginator = this.paginator;
}, error => {
console.log(error);
});
}
我无法理解错误在哪里。当我在控制台登录ELEMENT_DATA 变量以检查它的长度时,它显示undefined。
【问题讨论】:
-
确保您的 HTTP GET 成功返回一个值,
console.log(response);会有所帮助。 -
是的,我得到了正确的响应
标签: angular typescript http angular9