[英]ngFor is not showing the items in html? Angular 2/4
这真让我感到困扰。 我正在尝试以角度2/4显示数组。 我认为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
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.