6

I was trying to make a blog in angularJS and on the post message section I want to get the message from json and add it to a content div like this

<div class="content">
    {{json.message}}
</div>

Now my div has a paragraph in it, it's practically a html code like this

<p>this is my message</p>

but when i do this, i see on the screen this

<p>this is my message</p>

as text. I understand in previous versions i could use ng-bind-html-unsafe but i am using v1.2 of angularJS. Can anyone please show me code similar to ng-bind-html-unsafe so that I can make this work in v1.2? Thank you, Daniel!

1
  • 3
    Well I'll be damned bing has been used as a verb. Perhaps you meant bind? Commented Sep 12, 2013 at 20:39

2 Answers 2

6

You can use the Strict Contextual Escaping services ($sce) in 1.2

Controller:

function myCtrl($scope,$sce) {
    $scope.myHtml = $sce.trustAsHtml('<span>Hello World!</span>');
}

Template:

<div ng-app ng-controller="myCtrl">
    <div ng-bind-html="myHtml"></div>
</div>

Example: http://jsfiddle.net/TheSharpieOne/GKnrE/1/

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

1 Comment

thank you very much, this works perefect! also thank you @Problematic
4

You'll need to inject and use the $sce service to mark it as trusted HTML, then use the ng-bind-html directive (plunkr):

app.js:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $sce) {
  $scope.name = $sce.trustAsHtml('<p>Hello World</p>');
});

index.html:

<body ng-controller="MainCtrl">
    <div class="content" ng-bind-html="name"></div>
</body>

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.