javascriptangularjsyoutubeyoutube-apiangularjs-controller

how to display youtube thumbnail image from url with Angularjs


I just start to using angularjs and I want to display youtube thumbnail image from the youtube video url ... is there a way to display video thumbnail when people insert url in input and then click the button,

PLUNKER

http://plnkr.co/edit/9SBbTaDONuNXvOQ7lkWe?p=preview


Solution

  • Youtube provide default thumbnail image of its video.

    You can use below sample URL to create thumbnail image.

    http://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
    

    Where you need to search id from the given url & create url like above will give you thumbnail image.

    Controller

    app.controller('MyCtrl', ['$scope',
      function($scope) {
        $scope.inputs = [];
    
        $scope.addInput = function() {
          $scope.inputs.push({
            field: ''
          });
        }
    
        $scope.removeInput = function(index) {
          $scope.inputs.splice(index, 1);
        }
    
        $scope.set2 = function($ayd) {
          var thumb = getParameterByName(this.input.ayd, 'v'),
            url = 'http://img.youtube.com/vi/' + thumb + '/default.jpg';
          this.thumb = url
        }
    
        function getParameterByName(url, name) {
          name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
          var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(url);
          return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
      }
    ]);
    

    There many ways to do this, you can refer from here

    Working Plunkr Here