I am working on a custom AngularJS directive. How to bind an event to the dynamically generated html which is generated by the directive itself. Upon research I found that we need to use $watch and $compile, but I couldn't get it working. I found this answer relevant to my question. What is the alternative for jQuery ON in AngularJS
Any help is greatly appreciated. Please check this out in plunker
app.directive( 'btn', [ '$sce', function( $sce ){
return {
restrict: 'E',
replace: true,
template: '<div ng-bind-html="buttonHtml"></div>',
link : function ($scope, element, attrs) {
$scope.buttonHtml = $sce.trustAsHtml('<button ng-click="showMessage()">Click Me</button>');
$scope.showMessage = function () {
alert("You clicked Me");
}
} }; }]);
Thanks in advance