要在Angular中生成对象数组并显示数据,您可以按照以下步骤进行操作:
ng new my-angular-app
cd my-angular-app
ng generate component object-list
object-list.component.html
文件中添加以下代码:
Name
Age
City
{{ object.name }}
{{ object.age }}
{{ object.city }}
object-list.component.ts
文件中添加以下代码:import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-object-list',
templateUrl: './object-list.component.html',
styleUrls: ['./object-list.component.css']
})
export class ObjectListComponent implements OnInit {
objects: any[] = [
{ name: 'John', age: 25, city: 'New York' },
{ name: 'Jane', age: 30, city: 'London' },
{ name: 'Bob', age: 35, city: 'Paris' }
];
constructor() { }
ngOnInit() {
}
}
app.module.ts
文件中添加以下代码:import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ObjectListComponent } from './object-list/object-list.component';
@NgModule({
declarations: [
AppComponent,
ObjectListComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
文件中添加以下代码:
ng serve
http://localhost:4200
,您将看到一个带有对象数组数据的表格。这就是在Angular中生成对象数组并显示数据的基本过程。您可以根据自己的需求修改对象数组的数据和表格的样式。