Javascript 如何在不使用管道的情况下使用*ngFor访问对象键

Javascript 如何在不使用管道的情况下使用*ngFor访问对象键,javascript,angular,typescript,ecmascript-6,Javascript,Angular,Typescript,Ecmascript 6,我正在使用*ngFor循环遍历数据并生成一个动态表。但我只能访问对象的值,不能访问键。如何在不使用管道的情况下使用*ngFor访问对象键 export class AppComponent { users: {}[] = [{ firstName: 'John', lastName: 'Doe', email: 'johndoe@example.com' }, { firstName: 'Jane', lastName: 'Doe',

我正在使用
*ngFor
循环遍历数据并生成一个动态表。但我只能访问对象的值,不能访问键。如何在不使用管道的情况下使用
*ngFor
访问对象键

export class AppComponent {
  users: {}[] = [{
    firstName: 'John',
    lastName: 'Doe',
    email: 'johndoe@example.com'
  },
  {
    firstName: 'Jane',
    lastName: 'Doe',
    email: 'janedoe@example.com'
  },
  {
    firstName: 'Mary',
    lastName: 'Doe',
    email: 'marydoe@example.com'
  }
];
}

您可以使用它访问对象的密钥

*ngfor="let user of users; i=index"

由于您不想使用
管道
,因此只需使用
Object.keys获取密钥即可

export class App {
  users = [{
    firstName: 'John',
    lastName: 'Doe',
    email: 'johndoe@example.com'
  },
  {
    firstName: 'Jane',
    lastName: 'Doe',
    email: 'janedoe@example.com'
  },
  {
    firstName: 'Mary',
    lastName: 'Doe',
    email: 'marydoe@example.com'
  }
];
  getKeys = Object.keys;

}
和在模板中

 <div *ngFor="let key of getKeys(users[0])">{{key + ' : ' + users[0][key]}}
{{key+:'+用户[0][key]}

您可以在app.component.ts文件中使用Object.keys,该文件可能重复