I would like to assign different functions to ng-click within ng-repeat:
<button ng-repeat="button in notification.buttons" ng-click="button.fn"> {{button.label}} </button>
Controllers:
app.controller('MainCtrl', ['$scope', function($scope){
$scope.notification = {
ctrl: 'logout',
text: 'Logout?',
buttons: [
{label:'Yes', fn: 'logout()'},
{label:'No', fn: 'cancel()'},
],
};
}]);
app.controller('logout', ['$scope', function($scope){
$scope.logout = function(){
alert('logout');
};
$scope.cancel = function(){
alert('cancel');
};
}]);
If I put button.fn in double curlies ng-click="{{button.fn}}"
the generated html looks good in the Chrome inspector, but Angular throws the following error:
Error: [$parse:syntax] Syntax Error: Token 'button.fn' is unexpected, expecting [:] at column 3 of the expression [{{button.fn}}] starting at [button.fn}}].
If there are no double curlies, no error, but Angular generates the html like this and the functions won't fire:
<button ng-repeat="button in notification.buttons" ng-click="button.fn" class="ng-binding ng-scope"> Yes logout()</button>
I have a related question about setting ng-controller with scope variables with this very same example here.