iframe中的AngularJS ng-src

Tin*_*lle 30 javascript iframe angularjs

我在iframe中使用ng-src时遇到问题.我需要这样做:

<div class="tab-content">
        <ul class="nav nav-tabs" ng-repeat="document in issues.Document">
            <div class="tab-pane pdf-height col-md-5 padding_0" id="{{document.name}}">
                <iframe ng-src="http://192.168.223.110/cat/{{document.directory}}/{{document.name}}.{{document.type}}" height="100%" width="100%"></iframe>                    
            </div>
        </ul>
    </div>
Run Code Online (Sandbox Code Playgroud)

结果:

<iframe ng-src="http://192.168.223.110/cat/{{document.directory}}/{{document.name}}.{{document.type}}" height="100%" width="100%" src="http://192.168.223.110/cat/{{document.directory}}/{{document.name}}.{{document.type}}"></iframe>
Run Code Online (Sandbox Code Playgroud)

我知道问题是$ sce,这是对XSS的保护,并且链接需要添加到白名单......所以当我这样做时它正在工作.

<ul class="nav nav-tabs" ng-repeat="document in issues.Document">
    <div class="tab-pane pdf-height col-md-5 padding_0" id="{{document.name}}">
         <iframe ng-src="{{someUrl}}" height="100%" width="100%"></iframe>                    
     </div>
</ul>
Run Code Online (Sandbox Code Playgroud)

我在控制器内部定义:

$rootScope.someUrl = $sce.trustAsResourceUrl('http://192.168.223.110/cat/files/incoming/12345_3232ASD_pero.pdf');
Run Code Online (Sandbox Code Playgroud)

但我不能这样做因为我用ng-repeat循环,所以链接是动态生成的.它需要从数据库中读取!

Kop*_*lyf 79

您可以使用过滤器:

HTML:

<iframe src="{{yourURL | trustAsResourceUrl}}"></iframe>
Run Code Online (Sandbox Code Playgroud)

其中'yourURL'是iframe的URL,'trustAsResourceUrl'是过滤器,在某些模块中定义为(例如,filters-module):

JS:

angular.module('filters-module', [])
.filter('trustAsResourceUrl', ['$sce', function($sce) {
    return function(val) {
        return $sce.trustAsResourceUrl(val);
    };
}])
Run Code Online (Sandbox Code Playgroud)

您可以在应用程序的所有iframe和其他嵌入项中使用此过滤器.此过滤器将通过添加过滤器来处理您需要信任的所有URL.


Tin*_*lle 5

好的,我找到了问题所在。谢谢您的过滤器,它现在正在工作:)

我需要做的就是创建 ng-src 链接:

<iframe ng-src="{{apiUrl+document.directory + '/' + document.name + '.'+ document.type   | trustAsResourceUrl}}"
        height="100%" width="100%">
</iframe>
Run Code Online (Sandbox Code Playgroud)

也许这对某人有帮助!:)