第一种方式:原生JS向html页面添加内容节点元素代码段:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/angular.js/1.6.0/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="personCtrl"> <div id="test"></div> </div> <script> var app = angular.module('myApp', []); app.controller('personCtrl', function ($scope) { $scope.test = '<p><span style="border: 1px solid #000000;"><span style="text-decoration: line-through; font-family: 宋体, SimSun;"><em><strong>少时诵诗书所所所所所所所所所所所所所所所所所所所所所所所</strong></em></span><span style="text-decoration: line-through; font-family: 宋体, SimSun;"><em><strong><span style="text-decoration: line-through; font-family: 宋体, SimSun; border: 1px solid #000000;"></span></strong></em></span></span></p>' document.getElementById("test").innerHTML = $scope.test }); </script> </body> </html>
第二种方法:angular-sanitize方法向html页面添加内容节点元素代码段:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script> <script src="https://cdn.staticfile.org/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>