简体   繁体   English

如何从 TypeScript 对象获取数据?

[英]How to get data from an TypeScript Object?

Block of code in modal:模态代码块:

export class Profile {
    constructor(public id : number,public name : string, public age: number,public address:any)
}

Block of code in TypeScript file: 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')
  ];

How to get name id age as a list and navigate through JSON object array, because following block of code is not displaying anything as it is an array of objects in JSON:如何获取 name id age 作为列表并浏览 JSON 对象数组,因为以下代码块没有显示任何内容,因为它是 JSON 中的对象数组:

<div *ngFor="let p of profile">
    <ion-button>{{p.id}}</ion-button>
</div>

For this particular problem, it is better to use interface :对于这个特殊问题,最好使用interface

interface Profile {
id : number;
name : string; 
age: number;
address:any;
}

now use it like so :现在像这样使用它:

let profile : Profile[] = [{id : 1 , name : "Shahab" , age : 23 , adress : "Some where"} , ...] 

Now you can access it easily .现在您可以轻松访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM