Angular JS is one of the strengths of his data two-way binding this cow B function, we will often use two things is the Ng-bind and the Ng-model for the form. However, in our project we will encounter this situation, the data returned in the background with a variety of HTML tags. Such as:
$scope. currentwork.description = "hello,<br><b> where are we going today?" </b> "
We use instructions like ng-bind-html to bind, but the result is not what we want. That is true
Hello
Where are we going today?
What do we do?
For angular 1.2, we have to use the $SCE service to solve our problems. The so-called SCE is the abbreviation "Strict contextual escaping". Translated into Chinese is "strict context mode" can also be understood as a secure binding bar. Let's see how to use it.
Controller code:
$http. Get ('/api/work/get?workid= ' + $routeParams. WorkID). Success (function (work) {$scope. currentwork = Work;});
HTML Code:
<p> {{currentwork.description}}</p>
The content we return contains a series of HTML tags. The results are as we say at the beginning of the article. At this point we must tell it secure binding. It can be done by using $ sce.trustashtml (). This method converts the value to be accepted by the privilege and is safe to use "ng-bind-html". So, we have to introduce the $SCE service into our controller.
Controller'TRANSFERWORKSTEP2', ['$scope','$http','$routeParams','$sce', function ($scope, $http, $routeParams, $sce) {$http.Get('/api/work/get?workid='+$routeParams. WorkID). Success (function (work) {$scope. currentwork=Work ; $scope. Currentwork.description=$sce. trustashtml ($rootScope. currentwork.description);});
HTML code:
<p ng-bind-html= "Currentwork.description" ></p>
This results in a perfect presentation on the page:
Hello
Where are we going today?
We can also use this as a filter that can be called at any time on the template.
App.filter (' to_trusted ', [' $sce ', function ($SCE) {return function (text) { return $sce. trustashtml (text);};}]);
HTML code:
<p ng-bind-html= "Currentwork.description | To_trusted "></p>
In-depth understanding of ng-bind-html directives and $SCE services in Angularjs