LazioTibijczyk
LazioTibijczyk

Reputation: 1957

Function and variable assignment in conditional ng-click

I need to write a conditional ng-click with a function call and assignment. It's really important for me that this assignment is not written within a function because that function is quite generic.

I have 2 conditions I need to fulfil in order for ng-click function and assignment to be executed.

Here are some variations I have tried so far

<button ng-click="vm.hasPermission && vm.readOnly === false
                    ? vm.myFunction(); vm.buttonTouched = true
                    : null">
  My Button
</button>

<!-- As suggested in other stackoverflow thread (doesn't work) -->
<button ng-click="vm.hasPermission && vm.readOnly === false
                    ? (vm.myFunction(), vm.buttonTouched = true)
                    : null">
  My Button
</button>

<button ng-click="vm.hasPermission && vm.readOnly === false
                    ? vm.myFunction() && vm.buttonTouched = true
                    : null">
  My Button
</button>

The key bit is that I don't want vm.buttonTouched = true in vm.myFunction().

Upvotes: 0

Views: 63

Answers (2)

anima_incognita
anima_incognita

Reputation: 131

How about adding the button within an ng-if statement when you want the function called with the assignment? For example something like:

<button ng-if="vm.hasPermission && !vm.readOnly" ng-click="vm.myFunction(); vm.buttonTouched = true;"> My button </button>

<button ng-if="!vm.hasPermission || vm.readOnly" ng-click="do whatever you want here"> My button </button>

Upvotes: 0

joshua miller
joshua miller

Reputation: 1756

What about trying the following:

<button ng-click="vm.hasPermission && (vm.readOnly === false) && (vm.buttonTouched = true) && vm.myFunction()">
  My Button
</button>

Upvotes: 1

Related Questions