In-depth understanding of ng-bind-html directives and $SCE services in Angularjs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.