简体   繁体   English

AngularJS JSON检查对象

[英]AngularJS JSON Check Object

I am currently using Angular to read in a json file and output it onto a table. 我目前正在使用Angular读取json文件并将其输出到表中。

Because some of the objects are a little different, I want to make a check to see if job.text exists. 因为某些对象有些不同,所以我想检查一下job.text是否存在。

[
  {
    "job": {
      "href": "www.google.com",
      "text": "Google"
    },
    "api": "Some Text Here"
  },
  {
    "job": "Yahoo",
    "api": "More text here"
  }
]

If job.text exists, then display job.text else display job 如果存在job.text,则显示job.text,否则显示job

Here is my html from angular but only displaying objects with job.text, otherwise it returns nothing.Is there a simple way to write a check statement to make sure I can display both types of objects? 这是我的html文本,但仅显示具有job.text的对象,否则不返回任何内容。是否有一种简单的方法来编写检查语句以确保可以显示两种类型的对象?

<td><a ng-href="{{item.job.href}}" target="_blank">{{item.job.text}}</a></td>

Use a ternary: 使用三元:

{{item.job.text ? item.job.text : item.job}}

You should try and normalize your data struct a bit - seems odd that job may contain an object or a simple text field. 您应该尝试对数据结构进行一些标准化- job可能包含对象或简单文本字段,这似乎很奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM