2

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?

4
  • Are you using angular directives such as ng-if to manipulate the DOM? You should avoid manipulating it directly whenever possible. Commented Mar 16, 2017 at 10:08
  • No , trying to using controller function only Commented Mar 16, 2017 at 10:09
  • in js fiddle you can check its simple and straight forward requiremnet Commented Mar 16, 2017 at 10:11
  • solved jsfiddle.net/hmoju8ps/5 Commented Mar 16, 2017 at 10:20

3 Answers 3

2

It's not enough to just append an element into DOM: you'll have to let Angular know about it. One possible approach (listing only the controller here for brevity):

function Ctrl($scope, $compile) {
   var anchorTemplate = '<a ng-click="afterEvent()" href="#">anchor addedd </br> </a>';

   $scope.addingEvent = function(){
     var myEl = angular.element( document.querySelector( '#div' ) );
     var $anchor = angular.element(anchorTemplate);
     $compile($anchor)($scope);
     myEl.after($anchor);
   };
}

Here's the demo to play with, with some good practices applied as well. Yet, as a matter of fact, I'd rather have this functionality implemented as a directive (component in terms of Angular 1.5).

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

Comments

1

So, I have modified your code using ng-if statement. There is no need to remove or add elements in DOM using old JS ways.

HTML

<div ng-app="miniapp" ng-controller="Ctrl">

<div id="div" ng-click="addingEvent()">DIV <span><a ng-click="afterEvent()" href="#" ng-if="showAnchor">anchor addedd </a></span></div>


</div>

SCRIPT

var $scope;
var app = angular.module('miniapp', []);

function Ctrl($scope) {

            $scope.showAnchor=false;
      $scope.addingEvent = function(){

            $scope.showAnchor=true;

    };
     $scope.afterEvent = function(){
       alert('after event');
    };

};

1 Comment

Thanks Nitesh for giving time, i have given that example to demonstration purpose only , basically i have requirement to add anchor link dynamically on the base of API response .
1

Solved Jsfiddle demo updated

    myEl.append($compile('<a ng-click="afterEvent()" href="#">anchor addedd </br> </a>')($scope)); 

This will work

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.