I can't believe something this essential and easy was so hard to figure out with Angular. Googling like crazy I eventually came across this solution.
If you need to add a conditional statement if else inside the ng-click core directive, use the following structure:
1 |
ng-click="variable = (condition=='X' ? 'Y' : 'X')" |
Basically I wanted to assign a value to a variable on the ngClick event. It's fairly simple to use ternary operators to output strings or classes, it wasn't so obvious to assign variables.
Move logic to Controller / Directive
I hear you, all logic should be in a controller or directive. The view is not the place to be, I agree. However, sometimes you might need this 😉
Alex
It’s so stupid … “all logic should be in controller…” … Angular gives you this holy bunch of ng-click, ng-show, ng-everything and everyone is using it. Doesn’t they notice that this is also logic inside the view?
Rick
Agreed! 🙂
Muhammed Athimannil
In some cases it is useful solution 👍
Alberto Vieira
ty 🙂
Rick
You’re welcome Alberto!
Abhijan Pal
Its working. Thanks a ton!
Rick
You’re welcome!