How can I make an angularjs button click call a passed-in function? -


i'm trying make application has data-driven ui. so, i'm trying pass in function should called when button clicked. second button works, of course, explicitly calls function. however, first 1 doesn't. here's angular js code:

$scope.clickfunction = "clickedit()"; $scope.clickedit = function(){     alert("it worked!");} 

and html:

<p>how can make button call passed-in function?</p> <button ng-click="{{clickfunction}}">click me</button> <p>i'd evaluate this, works.</p> <button ng-click="clickedit()">click me</button> 

here's plunker code i'm try make work. http://plnkr.co/edit/arbx3bbghcv15mgrozx1?p=preview

you need define function name in controller want use event handler:

$scope.clickfunction = "clickedit"; 

then in template use bracket notation:

<button ng-click="this[clickfunction]()">click me</button> 

the idea this points current scope object, want, , that's why bracket notation useful here.

demo: http://plnkr.co/edit/tmdgucpnbyz3gawdi9tb?p=preview


Comments

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -