以角度4访问json对象并以html格式显示它

时间:2018-01-04 09:58:25

标签: json angular angular5

我有一个json对象如下。

{
  "CandidateSchemaRows": [
    {
      "Description": "sadasd",
      "Experience": 1,
      "type": "selectbox"
    },
    {
      "Description": "erwerw",
      "Experience": 2,
      "type": "selectbox"
    }
  ]
}

我想在打字稿的帮助下在html页面中打印它。 ts文件中的方法应该是什么?

3 个答案:

答案 0 :(得分:1)

使用ngFor

 <ul *ngFor="let pers of result?.CandidateSchemaRows">
        <li>
            {{pers.Description}} {{pers.Age}} {{pers.Experience}}     
        </li>
  </ul>

<强> DEMO STACKBLITZ

答案 1 :(得分:1)

组件代码:

@Component({
  selector: 'my-component',
  templateUrl: 'my-template.html'
})
export class MyComponent {
  let myObj = {
    "CandidateSchemaRows": [
      {
        "Description": "sadasd",
        "Experience": 1,
        "type": "selectbox"
      },
      {
        "Description": "erwerw",
        "Experience": 2,
        "type": "selectbox"
      }
    ]
  }
}

在您的HTML中:

<p>{{myObj.CandidateSchemaRows}}</p>

Angular guide

答案 2 :(得分:1)

ts文件中的

let data = {
  "CandidateSchemaRows": [
    {
      "Description": "sadasd",
      "Experience": 1,
      "type": "selectbox"
    },
    {
      "Description": "erwerw",
      "Experience": 2,
      "type": "selectbox"
    }
  ]
}

和HTML

<ul *ngFor="let values of data?.CandidateSchemaRows;let i=index">
        <li>
            <div class="description">{{values.Description}} of {{i}}</div>
            <div class="Experience">{{values.Experience}} of {{i}}</div>
            <div class="type">{{values.type}} of {{i}}</div>     
        </li>
  </ul>