gpt4 book ai didi

javascript - ngFor 没有显示 html 中的项目? Angular 2/4

转载 作者:行者123 更新时间:2023-12-03 04:18:08 25 4
gpt4 key购买 nike

这真的让我很烦恼。我正在尝试以 2/4 Angular 显示数组。我认为 Angular 有一个错误。 html、服务或组件部分,因为我的节点后端按其应有的方式工作。我的 html 看起来像这样。

<div class = "container">

<li *ngFor = "let customer of customers">
<div class = "col md-6">
{{ customer.firstName }}
</div>
</li>
<div *ngFor = "let customer of customers">
<div class = "col md-6">
{{ customer.lastName }}
</div>
</div>

我的服务是这样的

@Injectable()
export class CustomerService {

constructor(private http: Http) { }

//retreiving custmers
getCustomers(){
return this.http.get('http://localhost:3001/api/customers')
.map(res => res.json());
}
}

我的组件是这个

export class CustomersComponent implements OnInit {

customers: Customer[];
customer: Customer;
firstName: string;
lastName: string;

//initialize customer service
constructor(private customerService: CustomerService) { }

//initiated once component is loaded
ngOnInit() {

this.customerService.getCustomers()
.subscribe( customer =>
this.customer = customer);


}

最佳答案

您有一个拼写错误,请将customer更改为customers

this.customerService.getCustomers()
.subscribe( customers => {

this.customers = customers

});

}

关于javascript - ngFor 没有显示 html 中的项目? Angular 2/4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44059000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com