首页 > 解决方案 > 角度数据未显示在模板中

问题描述

我有带角度的 Ionic 应用程序,我得到了我的数据,constructor但无法打印它们html

代码

component

receiver: any;

constructor(
    //....
) {
    // get receiver data
    const receiverData = this.activatedRoute.snapshot.paramMap.get('receiverData');
    console.log('receiver data: ', receiverData); // does print the data (sample data below)
    this.receiver = receiverData;
}

HTML

<ion-title [innerHTML]="receiver?.username"></ion-title>

Sample data (returned by "console.log('receiver data: ', receiverData);"

receiver data:  {
  "id":4,
  "name":"Tester",
  "username":"Utester",
  "phone":"081000000000",
  "photo":null,
  "email":"tester@admin.com",
  "created_at":"2020-06-21T09:06:15.000000Z"
}

知道为什么我的数据没有以 html 格式打印吗?

标签: angulartypescriptionic-framework

解决方案


通常,如果它是对象,它可以正常工作而没有问题。但是您可以将字符串作为类型发送然后尝试解析它

 this.receiver = JSON.parse(receiverData);

推荐阅读