ng-src不能用于youtube嵌入视频

时间:2018-03-06 11:57:36

标签: angularjs

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

function(x, y) { function unholy(a, b) { if (typeof arguments[1] == 'string') { return "bye bye"; } else return a + b; } if (x == 0) unholy("bye bye"); else unholy(x + y); }

在我的控制器中, <iframe ng-src="{{ emedUrl }}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

嵌入代码无效。

3 个答案:

答案 0 :(得分:1)

我希望能解决你的问题:

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>

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

允许第三方网址使用$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>