如何从JSON文件嵌入youtube链接

时间:2015-07-13 05:47:15

标签: javascript angularjs ionic-framework youtube-javascript-api

我有像

这样的JSON文件
[{"id":"38", "youtube":"https://www.youtube.com/watch?v=PEnuHN-Mqvg"}]

我曾试过像

这样的php JavaScript
var str = data[i].youtube;
var res = str.split("?v="); 
//raplace & is  ?
var str2 = res[1];
var res2 = str2.replace("&", "?");
//asign iframe to url variable
var url = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/"+res2+"\"  frameborder=\"0\" allowfullscreen></iframe>";
$("#youtube").append(url); //show tag iframe

那么我如何在AngularJS中申请?

2 个答案:

答案 0 :(得分:3)

首先,我建议使用正则表达式从youtube网址获取视频ID。 str.split("?v=");无法在所有情况下使用,因为有效的YouTube视频网址可以采用以下任何一种形式:

其次,您需要为iframe构建html标记(嵌入代码),并使用有效的src和 bind 生成带有视图的结果字符串。像{{<iframe src="..">}}这样的简单插值不起作用。所以使用 ng-bind-html

<div ng-bind-html="<iframe src='...'>"></div>

最后,您需要$sce服务才能使AngularJS绑定,从而导致标记为可安全用于该上下文的值

<div ng-bind-html="getTrustedHTML('<iframe src=...>')"></div>

其中getTrustedHTML()是为角度上下文返回可信HTML的函数。

  $scope.getTrustedHTML=function(str){
       return $sce.trustAsHtml(str);
  }  

这是你的控制器应该是什么样子:

app.controller('MainCtrl', function($scope,$sce, yourService) {
  $scope.name = 'World';

  $scope.data = {};

  //If you want to get the video links from a JSON  
  yourService.getData().then(function(res){
      $scope.data = res.data;
  })  

  $scope.postContent = ''
  /* or directly use $scope.postContent = <youtube URL>
   */

  $scope.parseVideoURL = function(text) {
      var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
      return text.replace(re,
          '<iframe height="100%" width="100%" src="http://www.youtube.com/embed/$1" allowfullscreen></iframe>');
  }        

  $scope.publishVideo = function(vid){
    return $scope.parseVideoURL(vid)

  }


  $scope.getTrustedHTML=function(str){
       return $sce.trustAsHtml(str);
  }  
});

这里是标记

Enter a  Youtube URL. Eg: 'https://www.youtube.com/watch?v=PEnuHN-Mqvg'
<br><br><br>
<input type='text' ng-model="postContent" >
<div ng-bind-html="getTrustedHTML(publishVideo(postContent))"></div>

<h3>VIDEOS FROM JSON</h3>
<div ng-repeat="v in data" ng-bind-html="getTrustedHTML(publishVideo(v.youtube))"></div>

工作Plunkr

答案 1 :(得分:0)

var myApp = angular.module('myApp', []);

myApp.controller('yourController', function ($scope, testService) {

    $scope.data = {};
    testService.getData().then(data) {
        $scope.data = data;
        //loop through data I have direcctly passed 1  array element for eg 
        var str = data[0].youtube;
        var res = str.split("?v="); 
        //raplace & is  ?
        var str2 = res[1];
        var res2 = str2.replace("&", "?");
        //asign iframe to url variable
        $scope.url = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/"+res2+"\"  frameborder=\"0\" allowfullscreen></iframe>";//please use  
    });


});

myApp.service('yourService', function ($http) {

    this.getData = function () {
        return $http.get('data.json');
    }
});

在您的HTML上,如果您的div已经id为youtube(请通过此链接https://docs.angularjs.org/api/ng/directive/ngBindHtml

<div id="youtube" ng-bind-html = "url"></div>