How to make a true/false ternary operator simpler in a HTML template?


Below a partial html template of mine; I find it quite ugly…
Is there a way to make ${ === true ? true : false} ternary simpler?
I couldn’t find any way online.
Thanks a lot.

  <span data-desktop=${ === true ? true : false} data-mobile=${ === true ? true : false}>CITY</span>
  <span data-desktop=${dod.zipcode === true ? true : false} data-mobile=${dom.zipcode === true ? true : false}>ZIPCODE</span>
  <span data-desktop=${dod.building === true ? true : false} data-mobile=${dom.building === true ? true : false}>BUILDING</span>   


If you only wanted to pass true or false in data-desktop and data-mobile then you can do something like this:-

<span data-desktop=${!!} data-mobile=${!!>CITY</span>
  <span data-desktop=${!!dod.zipcode} data-mobile=${!!dom.zipcode}>ZIPCODE</span>
  <span data-desktop=${!!dod.building} data-mobile=${!!dom.building}>BUILDING</span>

Answered By – Shikhar Awasthi

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.