0

I am creating a forum where the replies are written with AngularJS (ng-repeat). A reply can contain links, and on all links, I have added a ng-click. This ng-click is not fired.

I guess it is because by the time the link is written to the dom, AngularJS is done hooking up functions. Are there any workarounds?

Here is an example: http://plnkr.co/edit/Jg0jVOpPL1LcVPHe5YQz?p=preview

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $sce) {
    $scope.htmlBody = '<a href="http://www.google.com" ng-click="testmethod()" target="_blank">This does not work</a>';

    $scope.toTrustedHtml = function(htmlCode) {
      return $sce.trustAsHtml(htmlCode);
    };

    $scope.testmethod = function()
    {
       alert('yes');
    }
});


<body ng-controller="MainCtrl">
   <a href="http://www.google.com" ng-click="testmethod()" target="_blank">This works</a>
   <br /><br />
   <p ng-bind-html="toTrustedHtml(htmlBody)"></p>
</body>

Thanks Rasmus

  • And your code is? How have you added an ng-click? – JB Nizet Sep 18 '14 at 10:53
  • You need to post some code if you want help, otherwise you'll just get downvotes... ng-click in a repeat should be fine, so you are not using Angular correctly – Lee Willis Sep 18 '14 at 11:09
  • have a look at this answer - http://stackoverflow.com/questions/22737927/angular-ng-bind-html-filters-out-ng-click – Lee Willis Sep 18 '14 at 11:54

1 Answers1

0

Here is a simple demo for your case: jsfiddle.net/HB7LU/6469/.

The idea is that you need to use $compile to let angular know you are adding dynamic functions.

f2lollpll
  • 997
  • 6
  • 15
ABOS
  • 3,723
  • 3
  • 17
  • 23