javascript - 为什么我的 $sce.trustAsHtml 内容呈现为纯文本?

标签 javascript angularjs

我不确定我在这里缺少什么,但由于某种原因,我的 iFrame 被呈现为文本而不是 HTML。我们使用的是 Angular 1.3.15。

这是我的观点:

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

这是我的 Controller :

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 注入(inject)到 Controller 中。我在日志中没有看到任何错误。我很茫然。有人可以帮忙吗?

谢谢!

最佳答案

使用ngSanitize

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

下载文件 - 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>

关于javascript - 为什么我的 $sce.trustAsHtml 内容呈现为纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612763/

相关文章:

javascript - 为什么在 $rootScope.$on 中需要 $rootScope.$apply?

javascript - 使用 AngularJS 的默认过滤器和用户定义过滤器的输入字段

jquery - 在 JQuery/Angular JS 中通过拖放创建嵌套条件

javascript - 使用自定义元素时 Firefox 中的递归过多

c# - 通过 JQuery 检查复选框时,复选框 OnCheckedChanged 不会触发

javascript - 处理多个 Ajax 请求

javascript - 将输入值格式化为小时和分钟格式 (hh :mm)

javascript - 当我循环测试用例时,为什么 Mocha 会跳过重新分配变量?

javascript - 是否可以在 MVC Controller 返回中调用模态弹出窗口(javascript)

javascript - AngularJS 数据表 Controller 标记