AngularJS ng-src inside of iframe

后端 未结 3 1960
长情又很酷
长情又很酷 2020-11-28 11:21

I have a problem using ng-src inside of an iframe. I need to do this:

相关标签:
3条回答
  • 2020-11-28 11:30

    Ok I found what the problem was.. Thank you for that filter it is working now :)

    All I needed to do was to create ng-src link this:

    <iframe ng-src="{{apiUrl+document.directory + '/' + document.name + '.'+ document.type   | trustAsResourceUrl}}"
            height="100%" width="100%">
    </iframe>
    

    Maybe this helps to someone! :)

    0 讨论(0)
  • 2020-11-28 11:35

    You can use a filter instead:

    HTML:

    <iframe src="{{yourURL | trustAsResourceUrl}}"></iframe>
    

    where 'yourURL' is the URL of the iframe and 'trustAsResourceUrl' is the filter and is defined as in some module(like eg. filters-module) as:

    JS:

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

    And you can use this filter in all the iframes and other embedded items in your application. This filter will take care of all the urls that you need to trust just by adding the filter.

    0 讨论(0)
  • 2020-11-28 11:45

    As what Kop4lyf said you need to add filter in js

    //Create a filter for trust url
        app.filter('trustAsResourceUrl', ['$sce', function($sce) {
        return function(val) {
            return $sce.trustAsResourceUrl(val);
        };
    }]);
    

    and output ih html as

    ng-src="{{x.title.rendered | trustAsResourceUrl}}"
    

    Some other filter :

    //Create a filter for trust html
        app.filter('trustAsHtml', ['$sce', function($sce) {
        return function(val) {
            return $sce.trustAsHtml(val);
        };
    }]);
    
    0 讨论(0)
提交回复
热议问题