1
我是Angular的新手,但我認爲我會通過使用Youtube API v3使自己的腳變得溼潤。我正在努力從一個頻道獲取播放列表,以顯示在html頁面的下拉菜單中。另外,從下拉列表中選擇播放列表時,我想顯示所選播放列表中每個視頻的標題和縮略圖。我是新來的,所以我會很感激一些幫助。使用Angular和Youtube API顯示播放列表和視頻詳細信息
以下是我有:
<html lang="en">
<head>
<title>Angular Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery-1.11.3.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/angular.min.js"></script>
<script>
var app = angular.module('ph', []);
var key = '';
app.factory('youtubeService', ['$http', function ($http) {
function getPlaylists(channelId) {
return $.get("https://www.googleapis.com/youtube/v3/channels", { params: {part: 'snippet', channelId: channelId, key: key} });
}
function getPlaylistVideos(id) {
return $http.get('https://www.googleapis.com/youtube/v3/videos', { params: { part: 'snippet', id: id, key: key } });
}
return { getPlaylists: getPlaylists, getPlaylistVideos: getPlaylistVideos }
}]);
app.controller('ctrl', ['$http', '$scope', 'youtubeService', function ($http, $scope, youtubeService) {
youtubeService.getPlaylists('UC1P0NQW6aixa5SUTbPF5CjQ').then(function (response) {
$scope.playlists = response.data.items;
});
$scope.getPlaylistVideos = function (selection) {
youtubeService.getPlaylistVideos(selection.snippets.id).then(function (response) {
$scope.playlistvideos = response.data.items;
});
}
}]);
</script>
</head>
<body ng-app="ph">
<div class="row">
<!-- BEGIN Playlist Section -->
<section id="playlistsection">
<div class="col-lg-12">
<div class="col-lg-6">
<table id="playlistvideostable" class="table table-responsive table-striped">
<thead>
<tr>
<th>Name</th>
<th>Thumbnail</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="video in playlistvideos">
<td>
{{video.snippet.title}}
</td>
<!--<td>
<img src="{{video.snippet.thumbnails.default.url}}" alt="Video Image"/>
</td>-->
</tr>
</tbody>
</table>
</div>
<div class="col-lg-6">
<h3>Available</h3>
<hr>
<select id="playlists"
ng-options="playlist.snippet.title for playlist in playlists track by playlist.snippet.id"
ng-model="data.selectedOption"
ng-change="getPlaylistVideos(data.selectedOption)"
class="form-control">
</select>
</div>
</div>
</section>
<!-- END Playlist Section -->
</div>
</body>
</html>
這裏是getPlaylists的例子帶回在JSON:
{
"kind": "youtube#channelListResponse",
"etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/mtu1ek5RgV1XsIgIoPS7GNv8NkI\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/TEgtFpLB_KhJ-XEwy-yOJneYIYw\"",
"id": "UC1P0NQW6aixa5SUTbPF5CjQ",
"snippet": {
"title": "Aaron Johnson",
"description": "",
"publishedAt": "2014-07-11T15:19:25.000Z",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/-py7sd_PnqBI/AAAAAAAAAAI/AAAAAAAAAAA/Q6pYI0EC7No/s88-c-k-no/photo.jpg"
},
"medium": {
"url": "https://yt3.ggpht.com/-py7sd_PnqBI/AAAAAAAAAAI/AAAAAAAAAAA/Q6pYI0EC7No/s240-c-k-no/photo.jpg"
},
"high": {
"url": "https://yt3.ggpht.com/-py7sd_PnqBI/AAAAAAAAAAI/AAAAAAAAAAA/Q6pYI0EC7No/s240-c-k-no/photo.jpg"
}
},
"localized": {
"title": "Aaron Johnson",
"description": ""
}
}
}
]
}
感謝之外。建議你消除/刪除你的YouTube密鑰儘快:) – Shaun