Summary of the usage of Angularjs ng-bind-html

Source: Internet
Author: User

$sanitize Services in the angular.

This service relies on the ngsanitize module. (This module needs to load the Angular-sanitize.js plugin)

To learn this service, you need to know another directive: ng-bing-html.

As the name implies, the difference between ng-bind-html and Ng-bind is that Ng-bind binds the value as a string, and the contents of the element, but ng-bind-html binds the value as HTML, and the element's HTML. Equivalent to the. Text () in JQ. and. HTML ().

However, for security reasons, if we use ng-bind-html directly will be error, ng-bind-html after the content must be processed.

There are two ways to handle this, one is to use the $SCE service, and the other is to use the $sanitize service. $sce Service How to use, in the future article will be independently explained, this article mainly explains $sanitize service.

$sanitize will cleanse the HTML tag by eradicating a whitelist. This way, unsafe content is not returned. The whitelist is based on $compileprovider's ahrefsanitizationwhitelist and imgsrcsanitizationwhitelist functions.

Like what

var app =angular.module (' myApp ', [' ngsanitize ']); App.controller (' Ctrl ', function ($scope, $sce) {    $scope. myhtml = ' <p style= "Color:blue" >an html\n ' +    ' <em onclick= "this.textcontent=\ ' code_bunny\ '" >click here</em >\n ' +    ' snippet</p> ';    $scope. trusthtml = $sce. trustashtml ($scope. myhtml)});

Summary of the usage of Angularjs ng-bind-html

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.