简体   繁体   English

Angular 在 ngOnInit 上分配 Oberservable

[英]Angular assign Oberservable on ngOnInit

I've got the following code:我有以下代码:

ngOnInit() {
    this.invoiceService.getIncorrectInvoices().subscribe(data => {
      this.incorrectInvoices = data;
      this.dataSource.data = this.incorrectInvoices
      console.log(this.incorrectInvoices);
    });
    console.log(this.incorrectInvoices);
  }

The problem is that console.log(this.incorrectInvoices);问题是console.log(this.incorrectInvoices); only gives the expected result on the first console.log which is executed second.仅在第二个执行的第一个console.log上给出预期结果。

I understand that this is because it is a async call, but how can I make sure that this.incorrectInvoices gets assigned with the data returned by the http call on ngOnInit()?我知道这是因为它是一个异步调用,但是我如何确保this.incorrectInvoices被分配了 ngOnInit() 上的 http 调用返回的数据?

this.incorrectInvoices does get assigned with the correct data. this.incorrectInvoices确实被分配了正确的数据。

You just cannot log it before it is resolved.在解决之前,您无法记录它。

One way to see it contains the data you need (besides debugging) would be to bind the property in the HTML template.查看它包含您需要的数据(除了调试)的一种方法是在 HTML 模板中绑定属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM