【发布时间】:2017-09-09 19:00:46
【问题描述】:
我正在使用 angular 4 和 ionic 3,我收到一个错误无法从我在 html 上显示的 json 获取属性“状态”未定义或空引用
这是我的家.ts:
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
data: Object;
userDetails : Object;
constructor(public navCtrl: NavController, public navParams: NavParams, public homeService: HomeServiceProvider, public app: App) {
this.homeService.home().then((result) => {
this.data = result ;
//localStorage.setItem('token', this.data.users_username);
this.userDetails = this.data['_body'];
console.log(this.data['_body']) ;
this.navCtrl.setRoot(MenuPage);
}, (err) => {
console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
});
}
dan ini home.html:
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<li>{{userDetails?.status}}</li>
</ion-content>
这是我的 json 结构:
{
"status": 1,
"message": "success"
}
有没有人可以帮助我并告诉我为什么? 谢谢
【问题讨论】:
-
根据这行代码
console.log(this.data['_body']) ;控制台记录了什么 -
我的 json 输出如下 { "status": 1, "message": "success" }
-
是json还是json-string输出?
-
我认为是 JSON