angularjs - Creating button with ng-repeat that has an array with functions -
i have array of objects 2 keys. key label
text go on button. , action
, function want happen when user clicks (ng-click
) button. when this, no error thrown, function not work :(
my js:
var mybtns = [{ label: 'btn1', action: function() { alert('hi') } }];
my html inside directive this:
<button type="button" ng-repeat="abtninfo in mybtns" ng-click="abtninfo.action()"> abtninfo.label </button>
here simple fiddle: http://jsfiddle.net/ppe34vlj/1/
on other hand if try <button ng-click="{{abtninfo.action}}">
, throws error:
> "error: [$parse:syntax] http://errors.angularjs.org/1.4.4/$parse/syntax?p0=%7b&p1=invalid%20key&p2=2&p3=%7b%7babtninfo.action%7d%7d&p4=%7babtninfo.action%7d%7d"
of course in situation array no longer function string:
var mybtns = [{ label: 'btn1', action: '$scope.restore()' }];
this simple fiddle shows second case: http://jsfiddle.net/ppe34vlj/3/
per comment above:
https://jsbin.com/vunevazita/1/edit?html,js,output
remember view
needs reference controller
via $scope
Comments
Post a Comment