About ANGULARJS automatically escaping HTML tags when binding data

Source: Internet
Author: User
Tags add time

Tortured for two days, and finally found that the answer is so simple, but the hard work is worth it, after all, in order to understand this and learn more code.

Angularjs in the data binding by default will be in the form of text output, that is, the HTML tags in your data are not escaped the full receipt, so as to improve security, prevent injection attacks in HTML tags, but sometimes needed, especially when reading formatted text from the database, cannot be displayed correctly on the page.

To escape HTML, use the Ng-bind-html property in the HTML tag of the data binding, which is dependent on the $sanitize, That is, you need to introduce the Angular-sanitize.js file and inject the service Ngsanitize when the module is defined. Like what:


<span ng-controller= "myctr" ng-bind-html = "Htmlstr" ></span>


function Myctr ($scope) {

$scope. htmlstr = ' <p style= ' color:red;font-size=18px; " ></p> ';


This allows for HTML escaping, but one problem is that the style tag is automatically filtered out by Angularjs, and in order to preserve it, you need to turn on non-secure mode.

But I have more problems than this, the above is only static to bind, and my own page is developed through the custom service $http asynchronous loading to get the data, and then in the success event for $scope binding data list, and in the template is used <div Ng-repeat= "article in articles" >...</div> is generated automatically by ANGULARJS, so the question is, how to let the automatically loaded data escape HTML tags.

The two days have been almost tried from the directive directive, Filter filter to $sanitize, $complie, but it has not been realized since the key point has not been noticed. Later found (or accidentally read a blog), ng-bind-html can also be bound to the data in the loop, that is, I have always thought that ng-bind-html can only be bound in the back-end $scope defined variables, that is, similar to the above mentioned example, And it can actually be so binding:


<div ng-repeat= "article in articles" >

<div class= "Panel-heading" >


<div class= "Panel-body" >

<article id= "Word-display" ng-bind-html= "article.content | Trusthtml ">





Success (function (data) {

$scope. articles = data;


Myapp.filter (' trusthtml ', function ($SCE) {

return function (Input) {

Return $SCE. trustashtml (input);



Where $sce is the ANGULARJS's own security processing module, the $SCE. trustashtml (Input) method is to parse and return the data contents in HTML form. Adding this filter to the data bound by ng-bind-html enables automatic escaping of HTML tags when data is loaded.



<divclass= "Panel Panel-default" ng-repeat= "alist in Cnc.list" id= "{{alist.id}}" > <divclass= "Panel-body" > <divclass= "Form-group" > Applicant:{{alist.adduser}}</div> <divclass= "Form-group" > Add time:{{alist.addtime}}</div> <divclass= "Form-group" > Effective time:{{alist.usetime}}</div> <divclass= "Form-group" > title:{{alist.title}}</div> <divclass= "Form-group" >Notification Content:<article ng-bind-html= "Alist.context |trusthtml" > </article> </div> <buttonclass= "Btn btn-success" ng-click= "cnc.approve (' y ', alist)" > Approval request </button> <buttonclass= "Btn Btn-danger" ng-click= "Cnc.approve (' n ', alist)" > Reject request </button> </div> </div>
       /* Custom filters (used to properly bind content with HTML tags to ng-model)        * Author:tai        *           /app.filter (' Trusthtml ',function($SCE) {               returnfunction  (input) {                    return  $sce. trustashtml (input);                }             }            );

About ANGULARJS automatically escaping HTML tags when binding data

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.