模态中的代码块:
export class Profile {
constructor(public id : number,public name : string, public age: number,public address:any)
}
TypeScript 文件中的代码块:
profile : Profile[]= [new Profile(1,'hello',4,'address'),new Profile(1,'hello',4,'address'),
new Profile(1,'hello',4,'address'),new Profile(1,'hello',4,'address')
];
如何以列表形式获取姓名 ID 年龄并浏览 JSON 对象数组,因为以下代码块不会显示任何内容,因为它是 JSON 中的对象数组:
<div *ngFor="let p of profile">
<ion-button>{{p.id}}</ion-button>
</div>
最佳答案
对于这个特定问题,最好使用接口(interface)
:
interface Profile {
id : number;
name : string;
age: number;
address:any;
}
现在像这样使用它:
let profile : Profile[] = [{id : 1 , name : "Shahab" , age : 23 , adress : "Some where"} , ...]
现在您可以轻松访问它。
关于javascript - 如何从 TypeScript 对象获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60165878/