绑定 <p> 内的 innerHTML 到变量 myText:
< 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 > |
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 | 指定要执行的变量或表达式。 |