无法将URL传递给离子控制器中的视图

时间:2016-07-11 09:44:58

标签: angularjs iframe ionic-framework youtube

我正在尝试将Youtube视频网址传递给播放Youtube视频的iframe视图。但是,当我打开视图时,它会显示一个白页,而不显示任何其他内容。

控制器

.controller('youtubeCtrl', function ($scope, $http, $ionicSideMenuDelegate){

  // Pop-up Navigation Menu
  $scope.openMenu = function () {
    $ionicSideMenuDelegate.toggleLeft();
  }

  $scope.video_url = "https://www.youtube.com/embed/LFTEUBHpFq0";
})

查看

<ion-view title="Youtube">
  <ion-nav-buttons side="left">
    <button class="button button-icon button-clear ion-navicon" ng-click="openMenu()"></button>
  </ion-nav-buttons>

  <ion-content>
    <div class="video-container">
        <iframe ng-src={{video_url}} frameborder="0" width="560" height="315" allowfullscreen></iframe>
    </div>
  </ion-content>
</ion-view>

我想通过&#39; video_url&#39;打开iframe并播放。但那并没有发生。我究竟做错了什么或不做什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我建议您需要在控制器中注入$sce服务,并在那里注入trustAsResourceUrl网址。

在您的控制器中。

.controller('youtubeCtrl', function ($scope, $http, $ionicSideMenuDelegate, $sce){

  // Pop-up Navigation Menu
  $scope.openMenu = function () {
    $ionicSideMenuDelegate.toggleLeft();
  }
  var videoSource = "https://www.youtube.com/embed/LFTEUBHpFq0"; 
  $scope.video_url = $sce.trustAsResourceUrl(videoSource)

})

工作Fiddle

希望这有帮助。