0

I'm trying to insert an HTML code from string into angular view, but all style attributes are being removed from the HTML

In the Controller:

$scope.htmlString = '<div style="height:40px;"></div><div style="height:40px;"></div><div style="height:40px;"></div>'

View:

<div ng-bind-html="htmlString"></div>

The result I'm getting is always the HTML without style attributes.

<div></div><div></div><div></div>

I have tried using ng-bind-html-unsafe directive with no success.

I appreciate any help.

4
  • why are you even trying to store this kind of HTML in a variable in the first place? Commented Oct 2, 2015 at 20:43
  • @Claies this is only an example, that HTML is generated by a function and I need to insert it as is. Commented Oct 2, 2015 at 20:47
  • generated by a function still doesn't say much. if it is an angular function, then there are many other better ways than this. If it is generated by the server, that's a code smell of it's own, as the server shouldn't be responsible for controlling the client view. also, there are better ways to get server content output than in a raw string variable. Commented Oct 2, 2015 at 20:51
  • Thank you for your comment, but @AliAdravi 's answer is exactly what I need. Commented Oct 2, 2015 at 20:57

1 Answer 1

2

see the fidddle

$scope.str = $sce.trustAsHtml(html);
<div ng-bind-html="str"></div>
Sign up to request clarification or add additional context in comments.

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.