0

How to add a function to a angular object dynamical to the onclick or click event?

Adding onclick={{item.function}} or ng-click={{item.function}} won't work

JS

angular
  .module('app', [])
  .controller('MainController', ['$scope', function($scope) {
    $scope.test = function() {
      alert(1);
    };
    $scope.items = {
      'item1': {
        name: 'Item1',
        function: 'test()' // Not calling
      }
    };
  }]);

Html

<html ng-app="app">
<body ng-controller="MainController">
  <ul ng-repeat="item in items">
    <li ng-bind="item.name" ng-click="item.function"></li>
  </ul>
</body>
</html>

Not working demo http://codepen.io/anon/pen/jqEKRP

1 Answer 1

2

In controller:

...
      'item1': {
        name: 'Item1',
        f: $scope.test
      }
...

In template:

<li ng-bind="item.name" ng-click="item.f()"></li>

http://codepen.io/bparnikel/pen/bpNjoE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.