0

I am trying to display the html tags from the data in the view.I have a data level which is $scope.level="<b>i should be bold</b>" and when the data is given in the template as given below should respect the html tag as well

<div ng-controller="MyCtrl" >
{{level}}
</div> 

that is it should be bold without using

<div ng-controller="MyCtrl" id="tableForVxp" class="dataDisplay2">
<b>{{level}}</b>
</div>

But with what i have tried so far i am not able to achive it.It is also showing the tag in the view.The issue is illustrated in this JSFiddle Here.

Is there anyway to achieve it.or am i totally wrong here?

2
  • you will need to use the Strict Conceptual Escaping service ($sce) and use ng-bind-html rather than expressions {{ }} Commented Jul 29, 2015 at 12:37
  • also, when submitting a fiddle with an issue, it is usually helpful to ensure that the version of angular in the fiddle is up to date. It is unlikely that you are working with angular 1.0.1 Commented Jul 29, 2015 at 12:50

4 Answers 4

2

You can use the ngBindHtml directive which evaluates the expression and inserts the resulting HTML into the element. Don't forget to include the ngSanitize directive.

https://docs.angularjs.org/api/ng/directive/ngBindHtml

Example :

app.js

angular.module('app', ['ngSanitize'])
.controller('Controller', function($scope) {
  $scope.data = '<b>my text</b>'
});

index.html

<div ng-controller="Controller">
 <p ng-bind-html="data"></p>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

You can use ng-bind-html-unsafe:

<div ng-controller="MyCtrl" id="tableForVxp" class="dataDisplay2">
    <div ng-bind-html-unsafe="level"></div>
</div>

Unsafe because the DOM should generally not be modified inside a controller.

Comments

0

Why can't you move the <b> tags to the markup and then just interpolate the value of 'level'?

http://jsfiddle.net/u37xtpjd/2/

Comments

0

You can accomplish this using the ng-bind-html directive:

<div ng-controller="MyCtrl" id="tableForVxp" class="dataDisplay2">
  <span ng-bind-html="level"></span>
</div>

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.