因此,我有一个页面,我需要根据在视图中单击的电影/意甲嵌入YouTube预告片中的YouTube预告片。为此,我使用YouTube搜索API。
现在加载页面时,它首先从另一个API获取一些数据,加载YouTube API,然后获取预告片的录像,然后将其分配给视图中的$scope
变量。
在视图中,我刚刚得到了这一点:
<div class="row">
<iframe width="420" height="315" ng-src="{{video}}" frameborder="0" allowfullscreen></iframe>
</div>
,控制器看起来像这样:
var serieId = $routeParams.id;
$http.get("http://api.tvmaze.com/shows/" + serieId)
.success(function (data) {
console.log(data);
$scope.name = data.name;
$scope.img = data.image.medium;
$scope.rating = data.rating;
initYTAPI();
$http.get("http://api.tvmaze.com/shows/" + serieId + "/episodes")
.success(function (episodes) {
console.log(episodes);
$scope.episodes = episodes;
var maxSeason = 0;
for (var i = 0; i < episodes.length; i++) {
if (episodes[i].season > maxSeason) {
maxSeason = episodes[i].season
}
}
$scope.seasonCount = [];
for (var i = 1; i <= maxSeason; i++) {
$scope.seasonCount.push(i)
}
});
});
和initytapi函数:
gapi.client.setApiKey("MY_API_KEY");
gapi.client.load("youtube", "v3", function(){
//yt api is ready
console.log("Api ready");
var query = $scope.name + " Official Trailer";
var request = gapi.client.youtube.search.list({
part: "snippet",
type: "video",
q: query,
maxResults: 1
});
request.execute(function(response){
var base_URL = "https://www.youtube.com/embed/";
var url = base_URL + response.items[0].id.videoId;
url = $sce.trustAs($sce.RESOURCE_URL, url);
console.log(url);
$scope.video = url;
//Tried to do it with ng-bing-html aswell
//$scope.trailerHtml = "<iframe width="420" height="315" ng-src="url" frameborder="0" allowfullscreen></iframe>";
});
});
我已经使用开发人员模式检查了页面,以查看iFrame
是否正确加载,但似乎不包含任何src
(或ng-src
)属性。
我认为这与JavaScript执行之前的视图渲染有关为了帮助。
所有帮助将不胜感激!
您的示波器变量($ scope.video)在Angular的事件循环之外被执行。您可以在$ scope.video(糟糕)上设置手表,也可以在$ scope中包装自定义范围更改。$ apply():
request.execute(function(response){
var base_URL = "https://www.youtube.com/embed/";
var url = base_URL + response.items[0].id.videoId;
url = $sce.trustAs($sce.RESOURCE_URL, url);
console.log(url);
$scope.$apply(function(){
$scope.video = url;
});
这是一本快速阅读,可以更好地理解Angular 1.3的事件循环。