使用 AngularJS 将 HTML 呈现在数据库中的视图中

Rendering HTML as it appears in the database in to a view using AngularJS

我正在使用 TinyMCE 为用户保存到数据库的内容设置样式,用户可以保存的内容示例如下:

<ol><li><em>This</em> is a <span style="text-decoration: underline;" data-mce-style="text-decoration: underline;">HTML</span> email that <strong><span style="font-family: 'times new roman', times;">should</span></strong> <span style="font-family: tahoma, arial, helvetica, sans-serif;">contain</span> different <span style="font-family: arial, helvetica, sans-serif;">fonts</span> in weights with lists and links...</li></ol>

我目前正在使用 ng-bind-html 在我的视图中显示此 HTML 内容,但是,它似乎正在剥离内联样式。这是我在浏览器中的来源:

<ol><li><em>This</em> is a <span>HTML</span> email that <strong><span>should</span></strong> <span>contain</span> different <span>fonts</span> in weights with lists and links...</li></ol>

有没有办法让我在视图中保留这些内联样式?

如果您使用的是 Angular 1.2+,您必须这样做:

$scope.htmlToBind = $sce.trustAsHtml(yourHtml);

有关详细信息,请参阅 documentation

您可以尝试将您的 HTML 标记为受信任,传入 $sce,如:

yourApp.controller('someCtrl', ['$scope', '$sce', function($scope, $sce) {      
  $scope.some_html = $sce.trustAsHtml(some_html_content);
}]);