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.
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).
To do this you need to use $sce angularjs built-in service to secure the HTML content before parsing. Controller: angular. module('myModule').
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With