How to link html in angular v1.2

I tried to create a blog in angularJS and in the post section of the post. I want to get a message from json and add it to a div div like this

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

Now my div has a paragraph, it’s almost HTML code like this

 <p>this is my message</p> 

but when I do this, I see it on the screen

<p>this is my message</p>

like text. I understand that in previous versions I could use ng-bind-html-unsafe, but I am using angularJS v1.2. Can someone show me code similar to ng-bind-html-unsafe so that I can do this work in v1.2? Thanks Daniel!

+6
source share
2 answers

You can use Strict Contextual Escaping ( $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/

+6
source

You will need to enter and use the $ sce service to mark it as reliable 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> 
+4
source

Source: https://habr.com/ru/post/953730/


All Articles