I'm trying to add new DOM element dynamically which has click event handler - already defined function. Here's the code:
HTML
<div ng-app="miniapp" ng-controller="Ctrl">
<div id="div" ng-click="addingEvent()">DIV </div>
</div>
JavaScript
var $scope;
var app = angular.module('miniapp', []);
function Ctrl($scope) {
$scope.addingEvent = function(){
var myEl = angular.element( document.querySelector( '#div' ) );
myEl.append('<a ng-click="afterEvent()" href="#">anchor addedd </br> </a>');
};
$scope.afterEvent = function(){
alert('after event');
};
};
Here's the demo. As you see, while the anchor is added, the click event is not handled correctly. How to fix it?
ng-ifto manipulate the DOM? You should avoid manipulating it directly whenever possible.