angularjs通过ng-bind-html指令和$sce服务绑定html
代码:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>angularjs嵌入html</title> </head> <body ng-app="app" ng-controller="MainCtrl"> <div ng-bind-html="someText"></div> <script src="./assets/angular/angular.min.js"></script> <script> angular .module('app', [], function () { }) .controller('MainCtrl', function ($scope, $sce) { $scope.someText = $sce.trustAsHtml('<div style="color: red;">hello, world</div>'); }); </script> </body> </html>
运行结果: