简体   繁体   中英

How do I get the property names of a class

I have a class Product. I wanna create a table which have column names same as class field names.
product.ts

export class Product{
 public id:number;
 public name:string;
 }

product.component.html

<th *ngFor="let column of columns">
 <tr>
     {{column}}
 </tr>

product.component.ts public columns = [];

This a JavaScript question rather than an Angular question.

The best resource for JavaScript and other browser platform documentation is https://developer.mozilla.org/en-US/

The specific answer is

Object.keys(instance)

This works for most JavaScript objects like class instances.

person = new Person(5, 'Joe Blow' );
columns = Object.keys(person);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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