[英]How to Iterate Observable response object from component in Angular 2
I calling the service from Component class and getting Observable (Customers) Object as response. 我从Component类调用服务,并获得Observable(Customers)Object作为响应。 If i use
<div *ngFor="let cust of customerDetailsItem">
{{cust.id}}
</div>
then i am getting the values. 如果我使用
<div *ngFor="let cust of customerDetailsItem">
{{cust.id}}
</div>
那么我正在获取值。 but if i want use that observable response in component then it showing " Undefined
". 但是,如果我想在组件中使用该可观察到的响应,则显示“
Undefined
”。
constructor(public service:ViewListCustomerServiceService) {
this.service.getCustomerItemList().subscribe(lst =>this.customerDetailsItem=(lst));
console.log(this.customerDetailsItem); //Showing Undefined.
}
because i want " customerDetailsItem
" for assign data to Json
object in Component class. 因为我想要“
customerDetailsItem
”将数据分配给Component类中的Json
对象。
Your object this.customerDetailsItem
will be undefined until you get a response from the subscription. 您的对象
this.customerDetailsItem
将是未定义的,直到您从订阅获得响应为止。 So what you can do is execute the code that is necessary after you receive the lst
. 因此,您可以做的是在收到
lst
之后执行必要的代码。
constructor(...) {
this.service.getCustomerItemList()
.subscribe(lst=>this.initialize(lst));
}
initialize(lst: any) {
this.customerDetailsItem = lst;
// from now on your object will be set
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.