Embed YouTube videos using ng-repeat in Angular

后端 未结 1 1316
迷失自我
迷失自我 2021-01-14 03:35

I am using ng-repeat directive in Angular to embed some videos fetched from server.

I get video IDs from server and I have following code:



        
相关标签:
1条回答
  • 2021-01-14 03:54

    Since 1.2 you can only bind one expression to *[src], *[ng-src] or action. You can read more about it here.

    Use ng-src and change your HTML to:

    ng-src="{{getIframeSrc(media.src)}}"
    

    In the controller add:

    $scope.getIframeSrc = function(src) {
      return 'https://www.youtube.com/embed/' + src;
    };
    

    Note that you need to specify the protocol.

    You also need to add the URL as trusted by configuring the $sceDelegate service:

    app.config(function($sceDelegateProvider) {
      $sceDelegateProvider.resourceUrlWhitelist([
        'self',
        'https://www.youtube.com/**'
      ]);
    });
    

    Demo: http://plnkr.co/edit/4U5HxNnVDwlF5udRiQa1?p=preview

    0 讨论(0)
提交回复
热议问题