I have a little problem with my angularJS code, I'm trying to display json that contain css and html code. All that display on my website is a hardcode form html+css(it looks like the code below), i tired to use an ng-bind-html/ng-bind-html-unsafe, but it only displays html, and css code just dispensary. The easiest way would be convert displayed text to a code, but i don't have any idea to do this.
-
your css is in <style> tag?Álvaro Touzón– Álvaro Touzón2017-02-14 09:59:24 +00:00Commented Feb 14, 2017 at 9:59
-
no, it's in the style for each element in html just like aboveLapsio– Lapsio2017-02-14 10:02:38 +00:00Commented Feb 14, 2017 at 10:02
-
the capture of image given, didnt see wellÁlvaro Touzón– Álvaro Touzón2017-02-14 10:03:34 +00:00Commented Feb 14, 2017 at 10:03
-
<p>asdsa</p> <ul> <li><span style="font-family: 'arial black', sans-serif;">asdsa</span></li> <li>d<span style="font-size: 24pt;">sad</span></li> <li>dsa</li> </ul> <ol> <li>asdsa</li> <li>adssa</li> <li>adsa</li> </ol> <blockquote> The whole code looks more or less like thisLapsio– Lapsio2017-02-14 10:12:07 +00:00Commented Feb 14, 2017 at 10:12
-
and the code is not compiled, and showed as raw content?Álvaro Touzón– Álvaro Touzón2017-02-14 10:13:37 +00:00Commented Feb 14, 2017 at 10:13
|
Show 1 more comment
1 Answer
You can use angularjs $compile service and do something like this in the controller-
First add a dependency to $compile in your controller and write below code
myHTML = '<p></p>'; // your JSON data
$("#MyDiv").append(myHTML);
var compiled = $compile(myHTML)($scope);
$("#MyDiv").html(compiled);
It is not a best practice to do DOM manipulation in controller though and instead you can create a directive to do this.
