【问题标题】:AngularJS - issue when embedding the Youtube video url from directive to scriptAngularJS - 将 Youtube 视频 url 从指令嵌入到脚本时出现问题
【发布时间】:2014-09-16 23:40:36
【问题描述】:

这是控制器中的工作代码PLUNKER

public JsonResult videohome()
        {
            BaseController bc = new BaseController();
            var video = (from n in bc.db.Video where n.Video == true select n).FirstOrDefault();
            var videos = new Videohome { youtubeid = "//www.youtube.com/embed/" + video.youtubeid };
            return Json(videos, JsonRequestBehavior.AllowGet);
        }
        public class Videohome
        {
            public string youtubeid { get; set; }
        }

它给出的输出为 {"youtubeid":"//www.youtube.com/embed/_kux-YQujjM"}

但是当我将此输出加载到我的脚本中时,它会显示错误,

<script>

        countryApp.controller('Homevideo', ['$scope', "$http",'$sce', function (scope, sce, http) {
            http.get('@Url.Content("/sample/videohome")').success(function (data) {
                $scope.video = data;
                $scope.videoUrl = $sce.trustAsResourceUrl($scope.video + $scope.video.youtubeid);
             });
         }]);
</script>
        <div ng-controller="Homevideo" > <br />         
         <iframe width="100%" height="250" ng-src="{{videoUrl}}" frameborder="0" allowfullscreen=""></iframe>
            <p>{{videoUrl}}</p>       
          </div>

我是否遗漏了代码中的任何内容,不胜感激。

【问题讨论】:

  • 您确定将 angular.js 附加到您的应用程序吗?

标签: javascript jquery angularjs youtube angularjs-directive


【解决方案1】:

这是一个工作版本(没有 http):http://plnkr.co/edit/6M0qdSNlUe1f7mhT7ASt?p=preview 我发现了一些错误,首先,尝试以相同的顺序并使用“$”前缀来组织导入:

countryApp.controller('Homevideo', ['$scope', "$http",'$sce', function ($scope, $http, $sce)

我注意到的第二件事是$scope.video + $scope.video.youtubeid,我认为你做得不对,$sce.trustAsResourceUrl($scope.video.youtubeid); 应该足够了,因为 $scope.video 是一个与 url 无关的 json 数组。

【讨论】:

  • 非常好,谢谢 此代码有助于解决我的问题... :)
猜你喜欢
  • 1970-01-01
  • 2014-03-01
  • 2011-07-05
  • 2016-01-19
  • 2010-11-10
  • 2022-10-13
  • 2018-09-10
  • 2013-09-02
  • 2011-07-04
相关资源
最近更新 更多