当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJS ng-bind-html 指令详解及实例代码

AngularJS ng-bind-html 指令详解及实例代码

2018年05月29日  | 移动技术网IT编程  | 我要评论

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 指令实例的详细介绍,有需要的朋友可以参考下。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网