Angularjs ng-bind-html usage summary, angularjsngbind
This article mainly explains the $ sanitize service in angular. This service depends on the ngSanitize module. (This module needs to load the angular-sanitize.js plug-in)
To learn about this service, you must first understand another command: ng-bing-html.
Random ().
However, for security reasons, if ng-bind-html is used directly, an error is returned. The content after ng-bind-html must be processed.
There are two Processing Methods: $ sce and $ sanitize. how to use the $ sce service will be explained independently in future articles. This article mainly describes the $ sanitize service.
$ Sanitize deletes a whitelist to purify html tags. Insecure content will not be returned. The whitelist is obtained based on the aHrefSanitizationWhitelist and imgSrcSanitizationWhitelist functions of $ compileProvider.
Let's look at a chestnut:
Html:
<!DOCTYPE html>
Js:
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)});
In this way, the content with html tags can be loaded in the div. The attributes of tags and events bound to elements will be retained.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.