9

in controller i have a variable say $scope.question.

$scope.question = "Hi this is <br/> book";

In html page is access this variable like {{question}}.

<div class="question">
        1. {{question}}
</div>

i want the output with line break... But instead it display as string... how to parse the html tags.

1

2 Answers 2

12

You should use ng-bind-html directive. To utilise that you need to:

//1. given
.controller('AppController', [
  function() {
   $scope.SomeHtml = '<b>some html</b>';


//2. use ng-bind
 <div ng-bind-html="SomeHtml"></div>

Check the demo from official AngularJS documentation on plnkr.co.

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

Comments

0

If our text contains some html content, then we have to use ng-bind-html directive in angular js, else our html content will not be parsed and will be rendered as it is.

Example:

If our text is

  $scope.Val = "<span> Something </span>"

and we write this text within curly brackets as follows:

  <div> {{Val}}  </div>

then the text will be rendered as it is i.e. Something

Therefore in order to get this html content parsed we have to write it as follows:

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

Now the text will be rendered as Something .

Note: Make sure to include ngSanitize module on your app eg:

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

Hope it helps...!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.