0

I am currently using Angular to read in a json file and output it onto a table.

Because some of the objects are a little different, I want to make a check to see if job.text exists.

[
  {
    "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

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?

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.