Ahmad Ali
Ahmad Ali

Reputation: 168

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 = [];

Upvotes: 1

Views: 90

Answers (1)

Martin
Martin

Reputation: 16292

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);

Upvotes: 1

Related Questions