Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a dynamic function on ng-click

Tags:

angularjs

I have button like this

<button ng-click="method = 'create'; title = 'Create'>create</button>
<button ng-click="method = 'update'; title = 'Update'>update</button>

and this

<button ng-click="create(param)">{{ title }}</button>
<button ng-click="update(param)">{{ title }}</button>

Code above works fine, but I want to make it dynamic.

so I change it like following.

<button ng-click="{{ method }}(param)">{{ title }}</button>

This does not work, I dont know why, although if I do inspect element, code above generates corrects code as mentioned before.

like image 729
Donny Gunawan Avatar asked May 15 '15 05:05

Donny Gunawan


1 Answers

Use of bracket notation:

<button ng-click="this[method](param)">{{ title }}</button>

this points to current scope object so with this[method] you can access method via variable name.

like image 30
dfsq Avatar answered Oct 06 '22 17:10

dfsq