Get data automatically parsed into HTML tags in angularjs

Source: Internet
Author: User
Tags html form

About ANGULARJS automatically escaping HTML tags when binding data
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,
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. 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 enables HTML escaping, but one problem is that the style tag is automatically filtered out by Angularjs, and in order to preserve these
it is necessary to turn on non-secure mode.
But I have more problems than that, but the above is just static binding, and my own page is to get the data by $http asynchronous loading in the custom service,
then bind the data list to $scope in the success event and use < in the template Div ng-repeat= "article in articles" >...</DIV>
is automatically generated by the 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, and can actually be so bound:
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.

Get data automatically parsed into HTML tags (go) 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.