Angular setting $sce in ng-repeat

2019-07-13 06:11发布

I am trying to display videos in iframe, but nothing gets displayed even though I am getting the right embed link for it. I have tried testing it by just displaying the link and the correct link gets displayed, and when I am hardcoding the same link for the iframe, videos gets displayed, but nothing gets rendered in the iframe when I am having it like this:

<ion-item ng-repeat="article in articles" class="item-light">
    <img ng-show="article.external_media.length == 0 || article.external_media.url == ''"  src="http://coop.app/imagecache/cover/{{article.cover_image}}">
    <iframe ng-show="article.external_media.length > 0 && article.external_media.url != ''" src="{{article.external_media[0].url}}"></iframe>
</ion-item>

Update

Since I need to inject $sce dependency I wonder how to apply it to all the possible links in my controller. How would that function look?

This is my controller:

.controller('FrontPageController', function($scope, ArticleService, $state) {
  ArticleService.all().then(function(data){
    $scope.articles = data;
})

1条回答
老娘就宠你
2楼-- · 2019-07-13 06:42

Since I need to inject $sce dependency I wonder how to apply it to all the possible links in my controller. How would that function look?

I'd recommend a filter for this.

.filter( 'safeUrl', [
    '$sce'
    function( $sce ){
        return function(url){
             //not sure which one you need here
             return $sce.trustAsUrl(url)
        }
    }
])

in your html

<iframe src="{{article.external_media[0].url | safeUrl}}">

I advocate filters over controller methods only in I like to keep my controllers very lightweight. If something needs to be interpreted, I use a filter.

查看更多
登录 后发表回答