如何使用Angular动态添加iframe src

时间:2016-05-19 02:55:33

标签: javascript angularjs iframe twitter-bootstrap-3

我遇到了Error: $interpolate:interr Interpolation Error,并将youtube视频动态地放入iframe中。

HTML

<div class="embed-responsive embed-responsive-16by9">
    <iframe class="embed-responsive-item" src="{{youtubeVideo}}" allowfullscreen></iframe>
</div>

的Javascript /角

$http({
    method: 'GET',
    url: getTankVideos(query)
}).then(function successCallback(response) {
    console.debug('response');
    console.debug(response);

    $scope.youtube = response;
    console.log('Youtube');
    console.log($scope.youtube);

    console.log('Video Url');
    $scope.youtubeVideo = 'https://www.youtube.com/watch?v=' + $scope.youtube.data.items[0].id.videoId;
    console.log($scope.youtubeVideo);

}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

我的调试语句给出了有效值

response
script.js:129 Object {data: Object, status: 200, config: Object, statusText: ""}
script.js:131 Youtube
script.js:132 Object {data: Object, status: 200, config: Object, statusText: ""}
script.js:134 Video Url
script.js:135 https://www.youtube.com/watch?v=BRtj_TSOHjw

1 个答案:

答案 0 :(得分:0)

为安全起见,您需要在控制器中注入$scope.youtubeVideo = $sce.trustAsResourceUrl('https://www.youtube.com/watch?v=' + $scope.youtube.data.items[0].id.videoId); 服务,并在那里注入trustAsResourceUrl。

{{1}}