packetie
packetie

Reputation: 5069

Does Angular allow ternary operator?

Got error with ng-style. Doesn't Angular allow ternary operator?

I saw a SO post that shows Angular does support ternary operator, but not sure why it doesn't work for me.

Error: [$parse:syntax] http://errors.angularjs.org/1.2.27/$parse/syntax?p0=%3A&p1=is%20an%20unexpected%20token&p2=6&p3=color%3Agray&p4=%3Agray
    at Error (native)
    at http://localhost/capstar/res/libs/angular/angular.min.js:6:450
    at gb.throwError (http://localhost/capstar/res/libs/angular/angular.min.js:170:252)
    at gb.parse (http://localhost/capstar/res/libs/angular/angular.min.js:169:110)
    at http://localhost/capstar/res/libs/angular/angular.min.js:99:443
    at m (http://localhost/capstar/res/libs/angular/angular.min.js:106:93)
    at h.$watch (http://localhost/capstar/res/libs/angular/angular.min.js:107:381)
    at http://localhost/capstar/res/libs/angular/angular.min.js:203:142
    at J (http://localhost/capstar/res/libs/angular/angular.min.js:54:373)
    at g (http://localhost/capstar/res/libs/angular/angular.min.js:47:256) <td ng-style="color:{{x.isBuiltIn? 'gray' : 'black'}}" class="ng-binding">

Upvotes: 1

Views: 1551

Answers (1)

Tamas Hegedus
Tamas Hegedus

Reputation: 29916

The error is not caused by the ternary operator, it gets evaluated as expected. You misuse ng-style, as it expects an expression which evaluates to a js object.

Any of the following should work:

<td style="color:{{x.isBuiltIn? 'gray' : 'black'}}" class="ng-binding">
<td ng-style="{ color: x.isBuiltIn? 'gray' : 'black' }" class="ng-binding">

Upvotes: 2

Related Questions