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