javascript - 如何从 TypeScript 对象获取数据?

标签 javascript html angular ionic-framework

模态中的代码块:

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/

相关文章:

javascript - 如何重用方法: define in main object and call from all component in VueJS

javascript - JS 中 Blob 对象和 File 对象有什么区别?

javascript - 当 JavaScript 执行时,Internet Explorer 6 中止页面请求

html - 多列的 css 属性命令仅适用于 Opera 和 IE,不适用于 firefox、chrome 或 safari

css - 如何让 div 高度自动调整为背景大小?

angular - 我认为变量不会反射(reflect)可观察值的值

javascript - 由多个组件组成的 react 形式

javascript - 如何在 then 语句中返回一组 promise

angular - super() 中的 typescript 依赖注入(inject)

javascript - 在触摸设备上捕获并停止拖动