最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Can I use a ternary operator with angularjs outside of the class attribute? - Stack Overflow

matteradmin2PV0评论

I'm looping through a json object with AngularJS, and having trouble dealing with nulls.

I'm new to Angular.

<a href="#" data-mycustom="{{product.related}}">...</a>

In the event of related being null:

{
    "related":null
}

I want to put a "-1" in its place:

<a href="#" data-mycustom="-1">...</a>

Tried ternary operator but it isn't evaluating... it's just displaying it as plain text.

I'm looping through a json object with AngularJS, and having trouble dealing with nulls.

I'm new to Angular.

<a href="#" data-mycustom="{{product.related}}">...</a>

In the event of related being null:

{
    "related":null
}

I want to put a "-1" in its place:

<a href="#" data-mycustom="-1">...</a>

Tried ternary operator but it isn't evaluating... it's just displaying it as plain text.

Share Improve this question edited Jul 18, 2015 at 12:44 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Apr 22, 2015 at 19:46 user1447679user1447679 3,2707 gold badges38 silver badges76 bronze badges 3
  • Make sure your angular is the latest version. The ternary operator is a fairly new addition. – anon Commented Apr 22, 2015 at 19:47
  • 2 Well I'm stumped. There are several alternative syntaxes, like {true: a, false: b}[condition], which I've tried and seem to work when ternary doesn't. You'd use that like any other angular statement. – anon Commented Apr 22, 2015 at 19:49
  • Oh.. my.. gosh. I figured out what I was doing wrong. Tenary works fine, if I put it all inside the curly brackets. – user1447679 Commented Apr 22, 2015 at 19:55
Add a ment  | 

4 Answers 4

Reset to default 3

Simply you could use ng-attr to add custom attribute after evaluation of {{}} interpolation directive

Markup

<a href="#" ng-attr-data-mycustom="{{product.related || '-1' }}">...</a>

For the sake of pleteness, this is the answer the OP's original question ("how do you use a ternary in a directive"):

Add {{}} around the entire ternary statement, not just the variable. For example, this

<a href="#" data-mycustom="{{product.related}} ? product.related : '-1'">...</a>

should be

<a href="#" data-mycustom="{{product.related ? product.related : '-1'}}">...</a>

Otherwise, Angular won't parse it -- you'll get the value of product.related, followed by the literal text ? product.related : '-1'.

use or operator. this code will help :

<a href="#" data-mycustom="{{product.related || -1}}">...</a>

Just use this

<a href="#" ng-attr-data-mycustom="{{product.related || '-1' }}">...</a>

It is something like "try to use product.related if it exists. if it doesnt, use -1 insted"

Post a comment

comment list (0)

  1. No comments so far