About ANGULARJS automatically escaping HTML tags when binding data (reprint-Black white)

Source: Internet
Author: User
Tags html form

Original: Http://okashii.lofter.com/post/1cba87e8_29e0fab
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:

Html:

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

Javascript:

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:

Html:

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

<div class= "Panel-heading" >

</div>

<div class= "Panel-body" >

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

</article>

</div>

</div>

Javascript:

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.

About ANGULARJS automatically escaping HTML tags when binding data (reprint-Black white)

Related Article

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.