为什么是我的$sce.将trustashml内容呈现为纯文本

Why is my $sce.trustAsHtml content being rendered as plain text?

本文关键字:文本 trustashml 我的 sce 为什么      更新时间:2023-09-26

我不确定我在这里错过了什么,但由于某种原因,我的iFrame被渲染为文本而不是HTML。我们使用的是Angular 1.3.15。

这是我的观点:

<span ng-bind-html="question.questionLabel"></span>

这是我的控制器:

vm.questionLabel = $sce.trustAsHtml('<iframe width="560" height="315" src="https://www.youtube.com/embed/3aL9qqWRm7E" frameborder="0" allowfullscreen></iframe>');

下面是以文本形式呈现在页面上的内容:

<iframe width="560" height="315" src="https://www.youtube.com/embed/3aL9qqWRm7E" frameborder="0" allowfullscreen></iframe>

我也注入了ngSanitize到控制器中。我在日志中没有看到错误。我很茫然。有人能帮帮我吗?

谢谢!

使用 ngSanitize

请查看此工作示例:http://plnkr.co/edit/RPOznM12iwVNZjv6MiO8?p=preview

下载file - angular-sanitize.js并将其包含到你的应用程序中。

var app = angular.module('myApp', ["ngSanitize"]);       
app.controller('myController', function($scope) {
    $scope.html = '<p>Your html code</p>';
});
<div ng-app="myApp">
     <div ng-controller="myController">
        <p ng-bind-html="html"></p>
     </div>
</div>

我错误地将ngSanitize注入到控制器中。

谢谢大家!