簡單的事件綁定
下面的html文件是舉例說明給模塊依賴綁定另一個模塊來凈化標(biāo)簽上的屬性
主要是體現(xiàn)模塊依賴綁定
<!DOCTYPE html>
<html class="no-js" ng-app="myApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./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 ng-controller="myController">
{{htmlStr}}
<hr>
<h1 ng-bind="htmlStr"></h1>
<h1 ng-bind-html="htmlStr"></h1>
<div ng-bind-html="s">
</div>
</body>
<script>
//sanitize 模塊 過濾凈化html字符串
//會將標(biāo)簽上的屬性全部移除, 只保留一個干凈的標(biāo)簽
//使用方法: 引入 angular-sanitize 腳本 在模塊中依賴 ngSanitize 就可以直接使用ng-bind-html
//內(nèi)置$sce 服務(wù), 用來新人某html字符串
//在控制器中依賴此服務(wù) 調(diào)用$sce.trustAsHtml(htnlStr)方法,信任傳入的html返回被信任的html,可使用ng-bind-html
//使用#sce信任html,請確保html是安全的(風(fēng)險自行承擔(dān))
angular.module("myApp",["ngSanitize"])
.controller("myController",["$scope","$sce",function($scope,$sce){
$scope.htmlStr =`
<div>
<p diy="自定義屬性">p 內(nèi)容</p>//
<span onclick="console.log(1)">span 內(nèi)容</span>
</div>
`;
$scope.s = $sce.trustAsHtml($scope.htmlStr);
}])
</script>
</html>```