I get a HTML-String from my database and I want to insert this into my AngularJs application via ng-bind-html. I did it like the following:
HTML:
<div ng-bind-html="myBindHtml"></div>
JavaScript:
$scope.myBindHtml = $sce.trustAsHtml(htmlStringToInsert);
My HTML (which I want to insert) looks like this:
<button ng-click="testClickEvent()">TestButton</button>
The insert works fine.
Now I wrote an function (testClickEvent) which the button should call. This simply outputs a string into the console.
But this part does not work. I guess there is no binding from my inserted HTML to this function. Is there any way to call my function?
Angular code will only execute when you run it in the angular context. When you use
ng-bind-htmlits generated outside the angular context/scope, so angular events likeng-clickdo not work, you need to rely on pure JS events likeonclickso refrain from these scenarios and just code them directly on angular due to complexity of solution!We can avoid
ng-bind-htmlwhen angular events are needed and we need to directly code them in the front end and not from input from databases or other methods