0

I am currently working with Angular 5 and jQuery. I created a dynamic div through jQuery which I appended to a div.

var push_div = '<div class="col-md-6">
                <span style="color: green;">' + 
                droppedItemID + '</span>&nbsp;
                <a href="javascript:void(0)" 
                (click)="removeContent()">X</a>
                </div>'
$(push_div).hide().appendTo("#" + dropZoneID).fadeIn(1000);

When I click on the link, the removeContent function is not calling.

removeContent() {
    console.log("function called");
}
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91

2 Answers2

1

Remove this -> href="javascript:void(0)" from <a> tag.

Vijay Prajapati
  • 738
  • 1
  • 7
  • 20
1

It's by design. Angular does not handle bindings defined in strings such as innetHTML bindings. (click) binding will not be processed in your situation.

You can do it like:

var dropZoneID = 'dropZone'
var droppedItemID = 'droppedItemID'

function removeContent() {
  console.log('removeContent');
}

var push_div = '<div class="col-md-6"><span style="color: green;">' + droppedItemID + '</span>&nbsp;<a href="javascript:void(0)">X</a></div>'
$(push_div).hide().appendTo("#" + dropZoneID).fadeIn(1000);

// Using $(document).on(...) to bind events to dynamically created elements
$(document).on('click', $(push_div).find('a').eq(0), function() {
  removeContent()
});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<div id="dropZone"></div>

For further details:

https://stackoverflow.com/a/37676847/1331040

https://stackoverflow.com/a/50809586/1331040

Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35