javascript - AngularJS 不读取 REST 上的 Iframe 元素

标签 javascript angularjs rendering

我的网页不只读取 Iframe 标记。我不知道为什么。 正如我在 postman 上检查的那样,内容发送正确。

这是我从 postman 收到的内容:

 "Conteudo": "<p>Test iframe:</p>\n\n<p><iframe frameborder=\"0\" height=\"300\" scrolling=\"no\" src=\"https://www.express.co.uk/news/science/923166/space-news-supernova-birth-astronomy-victor-buso\" width=\"300\"></iframe></p>\n\n<p>&nbsp;</p>\n\n<p>Check it OUt</p>\n",

我不知道为什么 html 上有一些“\”。

但是,当在我的控制台上显示相同的内容时,我收到以下结果:

<p>Test iframe:</p>
<p>
<iframe frameborder="0" height="300" scrolling="no" src="https://www.express.co.uk/news/science/923166/space-news-supernova-birth-astronomy-victor-buso" width="300"></iframe>
</p>

<p>Check it OUt</p>

但是当在 HTML 上显示时,我收到以下空元素: Unknown error

我对发生的事情有点困惑。谁能向我解释一下为什么我的导航器无法在 html 上呈现响应?

谢谢大家!

最佳答案

您需要通过运行 $sce.trustAsHtml 告诉 Angular 内容是安全的。在绑定(bind) HTML 字符串之前。请参阅下面的代码片段。

angular
  .module('myApp', [])
  .controller('myController', ['$scope', '$sce', function($scope, $sce) {
    $scope.Noticia = {};
    
    var content = "<p>Test iframe:</p>\n\n<p><iframe frameborder=\"0\" height=\"300\" scrolling=\"no\" src=\"https://www.express.co.uk/news/science/923166/space-news-supernova-birth-astronomy-victor-buso\" width=\"300\"></iframe></p>\n\n<p>&nbsp;</p>\n\n<p>Check it OUt</p>\n";
    
    $scope.loadContent = function() {
      $scope.Noticia.Conteudo = $sce.trustAsHtml(content);
    }
  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
  <div ng-bind-html="Noticia.Conteudo"></div>  

  <button type="button" ng-click="loadContent()">Load Content</button>
</div>

关于javascript - AngularJS 不读取 REST 上的 Iframe 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53010567/

相关文章:

javascript - 如何在 react 中设置标签的背景颜色?

javascript - ko observableArray 更新,但绑定(bind)选择列表未更新

AngularJs:使用 ng-value 复制数据但模型不更新

"SVG Tiny 1.2"在当今的移动和桌面浏览器中有何性能优势?

html - 网站在除 chrome 之外的所有浏览器上呈现完美

javascript - 使用 RequireJS 轮询可用资源

javascript - 弹出内容有时出现在主要内容后面

javascript - Ionic、angularjs、bootstrap 3 的左侧多级菜单无法正常工作

angularjs - 无法使 Angular 'staggering' 动画正常工作

html - 是什么阻止了 HTML 页面利用渐进式呈现?