Ionic 2 / Angular - 操纵JSON

时间:2017-03-31 16:55:34

标签: angularjs json ionic-framework

我在列出json文件时遇到了麻烦。

我的代码:

    this.http
      .post(link, data, options)
      .map(res => res.json().d)
      .subscribe(
          data => {
            this.profile = JSON.parse(data);
            console.log(this.profile);
          },
          err => {
            console.log("ERROR!: ", err);
          }
      );


Console.log打印这个:

对象{user:Object,class:Array(1)}

{  
   "user":{  
      "name":"Name",
      "date":"19880210",
      "email":"test@test.com",
      "about":"About me",
      "picuteUrl":"http://www.url.com/Midia/User/picture.jpeg",
   },
   "class":[  
      {  
         "id":"82",
         "name":"Class 01"
      }
   ]
}

我也可以在TypeScript文件中获取这些值

console.log(this.profile.user.name);
console.log(this.profile.class[0]);
console.log(this.profile.class[0].name);

但是在html代码中,我没有得到这个值。

<input type="text" value="{{profile.user.name}}">

ERROR:

ion-dev.js?v=1.1.4:156 TypeError: Cannot read property 'usuario' of undefined

1 个答案:

答案 0 :(得分:1)

您提供的代码中没有您正在访问的地方。您可以使用 elvis/safe 运算符来检查这些值是否存在

<input type="text" value="{{profile?.user?.name}}">