0

I building a basic drag and drop that has various prepared HTML that you can drag and drop into the page.

I got a object with the list of HTML that you can drag

element=[
 text: {html: '<p> asdasdasdas</p>'},
 list: {html: '<ul><li></li></ul>'};
 img: {html: '<img src="placeholder.jpg"/>'}
]

How to inject the html into the page?

Or should I be creating the HTML snippets as component and than having a referenced in the elements object to the component selector and injecting the angular selector and rendering the view?

1 Answer 1

1

You can use ng-bind-html to bind html

<div ng-bind-html="element.text.html"></div>
<div ng-bind-html="element.list.html"></div>
<div ng-bind-html="element.img.html"></div>

Notice: You have to include ngSanitize module on your app in order to use ng-bind-html

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

4 Comments

what happens if I got angular class inside that HTML and I bind the HTML would it bind the event listeners also?
You have to inject $sce to your controller and then set your html with trustAsHtml function. Example: $scope.htmlText = $sce.trustAsHtml($scope.element.text.html)
$scope is angular 1, I am talking about angular 2 buddy :)
Okay take a look at this post: stackoverflow.com/questions/32340641/…

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.