javascript Iframe 中的 AngularJs ng-src
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24660988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJs ng-src in Iframe
提问by Adam
I am attempting to set the ng-src of an iframe using a scope variable and it keeps coming through as blank.
我正在尝试使用范围变量设置 iframe 的 ng-src,但它一直显示为空白。
I tried this:
我试过这个:
<div ng-repeat="url in urls">
<div ng-click="testAlert(url.domain)">
<iframe ng-src="{{ url.domain }}" ></iframe>
<div style="text-align: center">{[ url.domain ]}</div>
</div>
</div>
The text shows up just fine, so I know the values are there as well as the click alerts the select domain. It is just the ng-src seems to end up blank and therefore doesn't pull up the site. If I hard code the ng-src to an external site it works.
文本显示得很好,所以我知道值在那里以及点击提醒选择的域。只是 ng-src 似乎最终是空白的,因此不会拉起该站点。如果我将 ng-src 硬编码到外部站点,它就可以工作。
回答by Patrick
Most likely has to do with $sce not being configured to trust the external resource when interpolated... Try putting this in your controller (be sure to inject $sceservice). trustAsResourceUrl is the method you will be interested in and you would pass the URL you want to use to that:
很可能与 $sce 未配置为在插入时信任外部资源有关...尝试将其放入您的控制器中(确保注入$sce服务)。trustAsResourceUrl 是您将感兴趣的方法,您可以将要使用的 URL 传递给该方法:
.controller("MainController", function ($scope, $sce) {
var urls = [];
//Need to trust resource to be able to interpolate, see $sce documentation
urls.push({domain: $sce.trustAsResourceUrl("http://angularjs.org")});
urls.push({domain: $sce.trustAsResourceUrl("http://www.jquery.com")});
$scope.urls = urls;
$scope.testAlert = function (value) {
alert(value);
}
});
See working fiddle.
请参阅工作小提琴。