Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ternary expression in ng-click in angularjs

I'm wondering if it's possible to use a ternary expression in the ng-click attribute. I don't want to use a separate controller function if possible.

It's a two button toggle setup - I can get a simple toggle working, but don't want a second click of the "off" button to turn back on.

The ternary in ng-click does not work (note the ternary in ng-class does work):

<button
    ng-click="allOn2==true ? allOn2 : !allOn2"
    ng-class="allOn2==true ? 'btn-green-on' : 'btn-green-off'">
ON</button>

Here's a more complete jsfiddle: toggler

like image 731
braddo Avatar asked Jun 19 '14 17:06

braddo


1 Answers

You are not doing anything with the ternary expression. For it to be useful, assign it:

{{ var1 }} {{ var2}}
<button
    ng-click="var1 = (allOn2==true ? allOn2 : !allOn2)"
    ng-class="{'btn-green-on':allOn2, 'btn-green-off' : !allOn2}">
ON</button>

I'm not sure how you can use ternary expressions for ng-class though...

like image 191
pixelbits Avatar answered Sep 22 '22 11:09

pixelbits