ngsrc不适用于youtube嵌入视频

2024-06-09 16:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我对youtube嵌入代码有一个小问题:

<iframe ng-src="{{ emedUrl }}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

在我的控制器中, $scope.emedUrl = "https://www.youtube.com/embed/<videId>";

嵌入代码不起作用


Tags: 代码httpssrcyoutube控制器ngmediaencrypted
3条回答

要允许第三方url,请使用$sce.trustAsResourceUrl()

<!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <body>

    <div ng-app="myApp" ng-controller="myCtrl">
    <iframe ng-app="myApp" ng-controller="myCtrl" ng-src="{{emedUrl}}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
    {{emedUrl}}
    </div>



    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $sce) {
        $scope.emedUrl = $sce.trustAsResourceUrl("https://www.youtube.com/embed/nhvxtxBBJVc");
    });
    </script>

    </body>
    </html>

正如@Mohsen所说,我在控制器中更改了这样的代码

$scope.embedUrl = $sce.trustAsResourceUrl('https://www.youtube.com/embed/'+videoId);

我希望它能解决您的问题:

angular.module('myApp')
  .filter('trustUrl', function ($sce) {
    return function(url) {
    return $sce.trustAsResourceUrl(url);
   };
});

然后在你的框架中:

<iframe ng-src="{{ emedUrl | trustUrl }}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

相关问题 更多 >