简体   繁体   English

如何从Angular 2中的组件迭代Observable响应对象

[英]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.

相关问题 如何在 Angular 组件中显示来自 Observable 的数据 - How to display data from Observable in Angular component Angular 4,将http响应observable转换为object observable - Angular 4, convert http response observable to object observable Angular 7变换对新可观察对象的可观察响应 - Angular 7 Transform Observable response to a new observable object 如何将响应 object 从 Angular 服务返回到组件的错误请求 - How to return response object for bad request from Angular service to component Angular Observable 从父组件到子组件获取更新的更改对象 - Angular Observable Get updated changed object from a parent to child component 如何从Angular 7中的Observable返回对象 - How to return object from Observable in Angular 7 如何以角度访问组件类中的可观察对象值? - How to access observable object values in component class in angular? Angular - 迭代来自 Google Books API 的响应对象 - Angular - Iterate over on response object from Google Books API Angular2:如何将每个对象迭代到可观察的包含对象数组数据? - Angular2: How can i iterate each object to an observable containing object array data? 在Angular 4中将API响应(对象)从组件解析到另一个组件 - Parsing API Response (object) from component to another component in Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM