2017-03-31 75 views
2

我无法列出json文件。Ionic 2/Angular - 操纵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打印这样的:

对象{用户:对象,类:阵列(1)}

{ 
    "user":{ 
     "name":"Name", 
     "date":"19880210", 
     "email":"[email protected]", 
     "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}}"> 

错误:

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

回答

1

没有您身在何处提供的代码访问,usuario。你可以使用elvis/safe操作员检查valaues存在

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

谢谢Sajeetharan。 现在它工作! 除了elvis操作符之外,是否还有另一种方法来加载对象,然后将其放入视图中,而不再存在此问题? – Wesley

+0

它工作异步的方式,所以你需要等待,直到你得到结果。你可以用preloader来处理。标记为答案,如果它有帮助 – Sajeetharan