0

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.

CODE

6
  • your css is in <style> tag? Commented Feb 14, 2017 at 9:59
  • no, it's in the style for each element in html just like above Commented Feb 14, 2017 at 10:02
  • the capture of image given, didnt see well Commented 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 this Commented Feb 14, 2017 at 10:12
  • and the code is not compiled, and showed as raw content? Commented Feb 14, 2017 at 10:13

1 Answer 1

1

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.

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.