Search code examples
angularjsangularjs-ng-click

Ternary condition in ng-click angular js


In HTML I have a button and I am using ng-click event on it, like this:

ng-click="!user.name : openModel('lg') ? ''"

I am saying if user.name is not defined then call the function called openModel() else do nothing.

But it is generating the error

Error: $parse:syntax
Syntax Error

Syntax Error: Token ':' is an unexpected token at column 33 of the expression [!user.name :] starting at [{4}].

So what is wrong here?

Thanks.


Solution

  • The correct syntax would be ng-click="!user.name ? openModel('lg') : angular.noop()". angular.noop() is a function that peforms no operations.