AngularJS ng-bind-html directive
AngularJS instance
Bind innerHTML within <p> to the variable myText:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="/libs//1.4.6/"></script> <script src="/libs//1.5.0-beta.0/"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <p ng-bind-html="myText"></p> </div> <script> var app = ("myApp", ['ngSanitize']); ("myCtrl", function($scope) { $ = "My name is: <h1>John Doe</h1>"; }); </script> <p><b>Notice:</b> This example contains "" document, 该document移除 HTML Danger codes in。</p> </body> </html>
Running results:
my name is:
John Doe
Notice:This instance contains the "" file, which removes dangerous code from HTML.
Definition and usage
ng-bind-html Directives are a safe way to bind content to HTML elements.
When you want AngularJS to write HTML in your application, you need to detect some dangerous code. By introducing the "" module into your application, the ngSanitize function is used to detect the security of the code. in your application you can do so by running the HTML code through the ngSanitize function.
grammar
<element ng-bind-html="expression"></element>
All HTML elements support this directive.
Parameter value
value | describe |
---|---|
expression | Specifies the variable or expression to be executed. |
The above is a detailed introduction to the AngularJS ng-bind-html instruction example. Friends who need it can refer to it.