以通用方式显示来自 api 的对象
Display objects which come from api in a generic way
有没有一种很好的 'generic' 方法从那个方法 return object
:
getItems(){
this.itemService.getAllWithPaging(this.params)
.subscribe(res => {
this.itemsAndPageableInfo = res;
});
}
然后以漂亮的方式显示 sub objects
?
我的 response
看起来像:
并且当我尝试显示 .number
时出现错误,这是 undefined
。 itemsAndPageableInfo
是这样声明的:itemsAndPageableInfo : any;
。所以这可能是 object
等,但我没有定义 sub objects
和其他属性。有没有办法不定义所有这些 sub objects
和显示或进一步使用 sub object
属性?
当然,我不想在显示或使用它之前定义所有这些 sub objects
和其他属性。
尝试将 *ngIf="itemsAndPageableInfo"
添加到显示数据的 html 元素。
您可以通过列出您的 属性
在 html 中加载子属性
{{itemsAndPageableInfo?.totalPages}}
您需要安全导航运算符 ?
以便您的代码在等待 http 请求完成时不会抛出错误(因为 itemsAndPageableInfo 在加载时未定义)
安全导航操作员文档 - https://angular.io/guide/template-syntax#safe-navigation-operator
有没有一种很好的 'generic' 方法从那个方法 return object
:
getItems(){
this.itemService.getAllWithPaging(this.params)
.subscribe(res => {
this.itemsAndPageableInfo = res;
});
}
然后以漂亮的方式显示 sub objects
?
我的 response
看起来像:
并且当我尝试显示 .number
时出现错误,这是 undefined
。 itemsAndPageableInfo
是这样声明的:itemsAndPageableInfo : any;
。所以这可能是 object
等,但我没有定义 sub objects
和其他属性。有没有办法不定义所有这些 sub objects
和显示或进一步使用 sub object
属性?
当然,我不想在显示或使用它之前定义所有这些 sub objects
和其他属性。
尝试将 *ngIf="itemsAndPageableInfo"
添加到显示数据的 html 元素。
您可以通过列出您的 属性
在 html 中加载子属性{{itemsAndPageableInfo?.totalPages}}
您需要安全导航运算符 ?
以便您的代码在等待 http 请求完成时不会抛出错误(因为 itemsAndPageableInfo 在加载时未定义)
安全导航操作员文档 - https://angular.io/guide/template-syntax#safe-navigation-operator