【问题标题】:How can I embed youtube Link from JSON file如何从 JSON 文件嵌入 youtube 链接
【发布时间】:2015-10-01 06:58:11
【问题描述】:

我有类似的 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中申请呢?

【问题讨论】:

  • 将 javascript 复制到 AngularJS 控制器中?
  • 完全没有,只是我的概念兄弟

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


【解决方案1】:

首先,我建议使用正则表达式从 youtube URL 获取视频 ID。 str.split("?v="); 并非在所有情况下都有效,因为有效的 youtube 视频 URL 可以具有以下任何形式:

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

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

最后,您需要 $sce 服务来进行 AngularJS 绑定,以产生一个标记为可安全用于该上下文的值

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

getTrustedHTML() 是一个函数,它为 Angular 的上下文返回可信的 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

【讨论】:

  • 很高兴它成功了。干杯,欢迎来到社区:)
  • 愿上帝保佑你兄弟 :)
【解决方案2】:
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');
    }
});

如果您的 div 有 youtube 的 id,则在您的 html 上(通过此链接查看它https://docs.angularjs.org/api/ng/directive/ngBindHtml

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

【讨论】:

  • 感谢您的支持:)
猜你喜欢
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
相关资源
最近更新 更多