About ANGULARJS automatically escaping HTML tags when binding data

Source: Internet
Author: User
Tags html form

About ANGULARJS binding data automatically escaping HTML tags after two days of torture, 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,
Prevents injection attacks in HTML tags, but is sometimes needed, especially when reading formatted text from a database and not displaying it properly in the page. and to escape HTML,
The Ng-bind-html property is used in the HTML tag of the data binding, which is dependent on the $sanitize, which is the need to introduce the Angular-sanitize.js file, and in the module definition
When injected into the service ngsanitize. For example: 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 enables HTML escaping, but one problem is that the style tag is automatically filtered out by Angularjs, and in order to preserve these
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 development of the page is through the custom service $http asynchronous loading to get the data,
The data list is then bound for $scope in the success event, and <div ng-repeat= "article in articles" is used in the template >...</div>
Generated automatically by ANGULARJS, so the question is, how do you let the automatically loaded data escape HTML tags? These two days from the directive Directive, Filter filter to $sanitize, $complie almost all tried, but because have not noticed the key point so
has not been implemented at all. Later found (or accidentally read a blog), ng-bind-html can also be bound to the data in the loop,
That is, I always thought that ng-bind-html can only be bound to the variables defined in the backend $scope, which is similar to the example mentioned above, but can actually be so bound: Html:<div ng-repeat= "article in Articles "> <div class=" panel-heading "> 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

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.