2

I have a array with some names of buttons and names of a function that is to be called when the button is clicked.

If I ng-repeat through the buttons everything works fine, except the function is not being executed. I'm not sure what else i can try, or even if it's possible.

Here is some data

$scope.something = [{
    name: 'Cool Button',
    func: 'test'
}, {
    name: 'Another button',
    func: 'something'
}]

and I'm using ng-repeat like so.

<button ng-click="some.func" ng-repeat="some in something">{{some.name}}</button>

Here are the things I have tried to get the function working.

some.func // Nothing happens
some.func() // Throws a error
{{ some.func }}() // Nothing Happens

here is one of the functions that's to be called

$scope.test = function() {
    alert('clicked');
};

Is this possible?

A Quick Fiddle I made.

2
  • Pass an actual function instead of a string... then just call some.func . Also, define the function you're calling above the handler. Commented Aug 10, 2013 at 17:19
  • stackoverflow.com/questions/16112445/… Commented Aug 10, 2013 at 17:22

1 Answer 1

2
ng-click="this[some.func]()"

or reference the function directly:

$scope.something = [{
    name: 'Cool Button',
    func: $scope.test
}, {
    name: 'Another button',
    func: $scope.anotherFunc
}]

ng-click="some.func()"
Sign up to request clarification or add additional context in comments.

3 Comments

Definitely doesn't work since you have a recursive reference to $scope.something
Oh come on, I'm just using the names he posted :p
this[some.func]() works perfectly for me, thanks. that is what I was looking for +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.