Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script>
    angular.module('app', []).directive('clickCount', function() {
      return {
        restrict: 'E',
        replace: true,
        compile: function(tElement) {
          return {
            pre: function(scope, iElement) {
              iElement.attr('ng-click', 'counter = counter +1'); // <- Add atribute
            },
            post: function(scope, iElement) {
              iElement.on('click', function() { // <- Perform business logic
                scope.$apply(function(){ // <- Since scope variables may be modified, don't forget to apply the scope changes
                  scope.$eval(iElement.attr('ng-click')); // <- Evaluate expression defined in ng-click attribute in context of scope
                });
              });
            }
          }
        }
      };
    });
  </script>
</head>
<body ng-app="app">
  <hr> The internal 'ng-click' does work:
  <click-count ng-repeat="X in ['A', 'B', 'C']" ng-init="counter = 0">
    {{ X }}, {{ counter }}
  </click-count>
  <hr> But an external 'ng-click' does work:
  <click-count ng-repeat="X in ['A', 'B', 'C']" ng-init="counter = 0" ng-click="counter = counter + 1">
    {{ X }}, {{ counter }}
  </click-count>
  <hr>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers