ng-bind-html指令AngularJS中的Java语言用于将HTML元素的innerHTML绑定到应用程序数据, 并从HTML字符串中删除危险代码。对于ng-bind-html指令, 必须使用$ sanitize服务。所有HTML元素都支持它。
语法如下:
<element ng-bind-html="expression"> Contents... </element>
例子:此示例说明了ng-bind-html指令的用法。
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>ng-bind-html Directive</title>
<script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
</script>
<script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-sanitize.min.js">
</script>
<style>
.green {
color: green;
font-size: 20px;
}
</style>
</head>
<body ng-controller="geek" style="text-align:center">
<h1 style="color:green;">lsbin</h1>
<h2 style="">ng-bind-html Directive</h2>
<p ng-bind-html="text"></p>
<script>
var myApp = angular.module("myApp", ['ngSanitize']);
myApp.controller("geek", ["$scope", function($scope) {
$scope.text =
"<span class='green'> lsbin</span> is the"
+ " computer science portal for geeks.";
}]);
</script>
</body>
</html>
输出如下:
更多前端开发相关内容请参考:lsbin - IT开发技术:https://www.lsbin.com/
参考更多AngularJS内容: