最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular js: Display an iframe in a pop up and see an empty frame - Stack Overflow

programmeradmin6浏览0评论

I am building an angularJs app.

When i am clicking on a photo i would like that a modal screen will be shown with a player for playing a YouTube clip.

When i type it hardcoded everything running fine. However when i get it from the server the iframe is empty.

In the controller

$scope.openVideoPlayerPopup = function (videoUrl) {
        var modalInstance = $modal.open({
            templateUrl: 'mon/partials/videoPlayerModalScreen.html',
            controller: 'VideoPlayerModalCtrl',
            resolve: {
                url: function () {
                  return videoUrl;
                }
            }
        });

        modalInstance.result.then(function () {

        }, function () {
            console.log('Modal dismissed at: ' + new Date());
        });
    }; 

VideoPlayerModalController:

elbitApp.controller('VideoPlayerModalCtrl', ['$scope', '$modalInstance', 'url',
                                     function ($scope, $modalInstance,url) {

    $scope.givenUrl =  url;                        

    $scope.ok = function () {
        $modalInstance.close();
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

videoPlayerModalScreen.html:

<div class="modal-header">
    <h3 class="modal-title">Video Player</h3>
</div>
<div class="modal-body-video">
    <iframe width="420" height="345" ng-src='{{givenUrl}}'></iframe>
    <!--""-->
</div>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="ok()">OK</button>
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>

If we put instead{{givenurl}}, ng-src="" It will run fine...

What am i doing wrong?

I am building an angularJs app.

When i am clicking on a photo i would like that a modal screen will be shown with a player for playing a YouTube clip.

When i type it hardcoded everything running fine. However when i get it from the server the iframe is empty.

In the controller

$scope.openVideoPlayerPopup = function (videoUrl) {
        var modalInstance = $modal.open({
            templateUrl: 'mon/partials/videoPlayerModalScreen.html',
            controller: 'VideoPlayerModalCtrl',
            resolve: {
                url: function () {
                  return videoUrl;
                }
            }
        });

        modalInstance.result.then(function () {

        }, function () {
            console.log('Modal dismissed at: ' + new Date());
        });
    }; 

VideoPlayerModalController:

elbitApp.controller('VideoPlayerModalCtrl', ['$scope', '$modalInstance', 'url',
                                     function ($scope, $modalInstance,url) {

    $scope.givenUrl =  url;                        

    $scope.ok = function () {
        $modalInstance.close();
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

videoPlayerModalScreen.html:

<div class="modal-header">
    <h3 class="modal-title">Video Player</h3>
</div>
<div class="modal-body-video">
    <iframe width="420" height="345" ng-src='{{givenUrl}}'></iframe>
    <!--"https://www.youtube./embed/3O1_3zBUKM8"-->
</div>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="ok()">OK</button>
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>

If we put instead{{givenurl}}, ng-src="https://www.youtube./embed/3O1_3zBUKM8" It will run fine...

What am i doing wrong?

Share Improve this question asked Aug 5, 2014 at 11:38 AviadeAviade 2,0974 gold badges27 silver badges49 bronze badges 3
  • Are you using dependency injection to pass in the url? Could you maybe make it into a plnkr ? – Pieter Herroelen Commented Aug 5, 2014 at 11:53
  • jsfiddle/Hx7B9/2 something similar... – Aviade Commented Aug 5, 2014 at 12:28
  • Yes this is somewhat similar. To find a solution for your problem it would be nice though if you could reproduce your problem with a jsfiddle ... – Pieter Herroelen Commented Aug 5, 2014 at 12:48
Add a ment  | 

2 Answers 2

Reset to default 8

I found the problem. I am using iframe. Angular requires to use $sce when we are using iframe.

The solution:

app.controller('VideoPlayerModalCtrl', ['$scope', '$modalInstance', '$sce', 'url',
                                     function ($scope, $modalInstance, $sce, url) {


    $scope.givenUrl =  $sce.trustAsResourceUrl(url);

    $scope.ok = function () {
        $modalInstance.close();
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

Instead of $scope.givenUrl = url replace it in $scope.givenUrl = $sce.trustAsResourceUrl(url). Don't forget to add $sce as a dependency injection.

I have also ran into this recently. Here is my solution using a filter:

Filter:

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

In the HTML:

<iframe width="420" height="315" ng-src="{{PassedInUrlGoesHere | trustAsResourceUrl}}"></iframe>
发布评论

评论列表(0)

  1. 暂无评论