AngularJS ng-bind-html 指令详解及实例代码
- 作者: 健力宝
- 来源: 51数据库
- 2021-08-24
angularjs ng-bind-html 指令
angularjs 实例
绑定 <p> 内的 innerhtml 到变量 mytext:
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="http://www.51sjk.com/Upload/Articles/1/0/268/268333_20210708023631402.js"></script> <script src="http://www.51sjk.com/Upload/Articles/1/0/268/268333_20210708023631416.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 指令实例的详细介绍,有需要的朋友可以参考下。
推荐阅读
热点文章
Angular中响应式表单的三种更新值方法详析
7
Angularjs实现下拉框联动的示例代码
7
详解AngularJS跨页面传值(ui-router)
2
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
3
angular2路由切换改变页面title的示例代码
4
Angular2 组件间通过@Input @Output通讯示例
5
Angularjs中ng-repeat的简单实例
3
AngularJS 中ui-view传参的实例详解
4
浅谈Angular路由守卫
4
详解基于Angular4+ server render(服务端渲染)开发教程
4