AngularJS ng-bind-html 指令详解及实例代码
程序员文章站
2022-04-09 17:13:49
angularjs ng-bind-html 指令
angularjs 实例
绑定
内的 innerhtml 到变量 mytext:...
angularjs ng-bind-html 指令
angularjs 实例
绑定 <p> 内的 innerhtml 到变量 mytext:
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <script src="http://apps.bdimg.com/libs/angular.js/1.5.0-beta.0/angular-sanitize.min.js"></script> </head> <body> <div ng-app="myapp" ng-controller="myctrl"> <p ng-bind-html="mytext"></p> </div> <script> var app = angular.module("myapp", ['ngsanitize']); app.controller("myctrl", function($scope) { $scope.mytext = "my name is: <h1>john doe</h1>"; }); </script> <p><b>注意:</b> 该实例包含了 "angular-sanitize.js" 文件, 该文件移除 html 中的危险代码。</p> </body> </html>
运行结果:
my name is:
john doe
注意:该实例包含了 "angular-sanitize.js" 文件, 该文件移除 html 中的危险代码。
定义和用法
ng-bind-html 指令是通一个安全的方式将内容绑定到 html 元素上。
当你想让 angularjs 在你的应用中写入 html,你就需要去检测一些危险代码。通过在应用中引入 "angular-santize.js" 模块,使用 ngsanitize 函数来检测代码的安全性。 in your application you can do so by running the html code through the ngsanitize function.
语法
<element ng-bind-html="expression"></element>
所有的 html 元素都支持该指令。
参数值
值 | 描述 |
---|---|
expression | 指定要执行的变量或表达式。 |
以上就是angularjs ng-bind-html 指令实例的详细介绍,有需要的朋友可以参考下。