简体   繁体   English

离子模板中的 var 未定义,但 console.log 显示它

[英]var in ionic template undefined, but console.log displays it

I get an undefined error from the call in the ionic template:我从离子模板中的调用中得到一个未定义的错误:

ERROR TypeError: "this.x is undefined"错误类型错误:“this.x 未定义”

But when I log this.x to the console it looks fine.但是当我将 this.x 登录到控制台时,它看起来很好。

Maybe it's an easy problem, but I just started learning this.也许这是一个简单的问题,但我刚刚开始学习这个。 If anyone can help, it would be appreciated :)如果有人可以提供帮助,将不胜感激:)

this.http.get('xy.json', {responseType: 'text'})
    .subscribe(response => {
      this.x = JSON.parse(response);
      console.log(this.x);
});

getCurrentObj() {
    return this.x[0];
}

Template: {{ getCurrentObj().text }}模板: {{ getCurrentObj().text }}

Json:杰森:

{
    "0": {
        "text"    : "This is sample text 1",
        "type"    : "xy"
    }
}

this.x from console.log:来自 console.log 的 this.x:

Object(1)
​
0: Object { text: "This is sample text 1", type: "xy", … }
​
<prototype>: Object { … }

The main problem is that you are trying to render data which are not loaded yet.主要问题是您正在尝试呈现尚未加载的数据。

subscribe() is an asynchronous function, so you have to wait for the data from there. subscribe()是一个异步函数,所以你必须等待那里的数据。 There are two ways to do it.有两种方法可以做到。

  1. Wrap your {{ getCurrentObj().text }} into <ng-container *ngIf="x"></ng-container> => <ng-container *ngIf="x">{{ getCurrentObj().text }}</ng-container>将您的{{ getCurrentObj().text }}包裹到<ng-container *ngIf="x"></ng-container> => <ng-container *ngIf="x">{{ getCurrentObj().text }}</ng-container>
  2. You can return an Observable from the function where you do the get request and then use it with the async pipe in the template =>您可以从执行get请求的函数返回一个 Observable,然后将它与模板中的async管道一起使用 =>

component:成分:

getCurrentObj(): Observable<CORRECT_INTERFACE_HERE> {
    return this.http.get('xy.json', {responseType: 'text'});
}

template:模板:

{{ (getCurrentObj() | async)?.text }}

Another thing is that you don't need JSON.parse(response) as the httpClient do it for you.另一件事是你不需要JSON.parse(response)因为httpClient为你做。

You should display data like this你应该像这样显示数据

get getCurrentObj() {
    return this.x[0];
}

Then in your template然后在你的模板中

{{getCurrentObj}}

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

相关问题 falsey var在console.log中显示未定义 - falsey var showing undefined in console.log Mozilla Firebug console.log() 将答案显示为未定义 - Mozilla Firebug console.log() displays the answer as undefined JavaScript let变量在console.log中显示为未定义 - JavaScript let variable displays as undefined in console.log JS:console.log 在对象数组的末尾显示“未定义” - JS : console.log displays "undefined" in the end of an array of objects Console.log 未定义 - Console.log is undefined 在Ionic3中订阅提供程序后,Console.log显示为未定义 - Console.log shows as undefined after subscribing to provider in Ionic3 “ console.log(“ var:“ + var)”的简写? - Shorthand for “console.log(”var: “ + var)”? 未定义而 console.log() - Undefined while console.log() console.log(String(console.log(&#39;Not undefined&#39;))===&#39;未定义&#39;); console.log(String(console.log(&#39;Not undefined&#39;))!==&#39;Not undefined&#39;); - console.log(String(console.log('Not undefined')) === 'undefined'); console.log(String(console.log('Not undefined')) !== 'Not undefined'); console.log()显示正确的值,但是将其分配给变量时,该值未定义-Angular,firestore - console.log() displays the correct value, but when assigned it to a variable, it is undefined - Angular, firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM