I have a really simple angular app. It just adds 1 to the value on ng-click.
See JSFiddle
<div ng-app="app" ng-controller="ctrl">
<div ng-click="hello=hello+1">THIS WORKS ON CLICK: {{hello}}</div>
<div ng-click="hello++">THIS DOESN'T: {{hello}}</div>
</div>
I expected to be able to '++' the value of a variable using ng-click, but apparently not. Am I doing something incorrectly?
The ng-click attribute expects a angular expression. While these expressions are javascript like, they are not javascript and you cannot do everything you can do in javascript in them. For more information, look at https://docs.angularjs.org/guide/expression
The important part from the link would be:
Angular does not use JavaScript's eval() to evaluate expressions. Instead Angular's $parse service processes these expressions.
Angular expressions do not have access to global variables like window, document or location. This restriction is intentional. It prevents accidental access to the global state – a common source of subtle bugs.
Instead use services like $window and $location in functions called from expressions. Such services provide mockable access to globals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With