【问题标题】:unable to get property 'status' undefined or null refrence Angular 4无法获得属性“状态”未定义或空参考Angular 4
【发布时间】: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

标签: angular ionic-framework


【解决方案1】:

如果您的 json 如上所述,它应该可以工作。如果不尝试解析它然后显示它

this.userDetails = JSON.parse(this.data['_body']);

【讨论】:

    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    相关资源
    最近更新 更多