Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse html tags inside string in angular js

Tags:

angularjs

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.

like image 202
bharath Avatar asked Jul 09 '15 11:07

bharath


People also ask

How do I render a string with HTML tag in angular 8?

To render a html string in angular, we can use the innerHTML property by binding a string to it. The innerHTML property sanitizes the html, so that your app is safe from the cross-site scripting attacks(XSS).

Which angular module can you use to parse HTML data securely?

To do this you need to use $sce angularjs built-in service to secure the HTML content before parsing. Controller: angular. module('myModule').

How get data from Angular JS to HTML?

Create an app and its controller scope. Define the controller variable. Call the app and controller to the body of HTML. Inside the body use span tag and use attribute ng-bind-html and assign the value as the scope variable.


1 Answers

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.

like image 77
shershen Avatar answered Oct 26 '22 14:10

shershen