我试图将showResponse函数中的videoUrl变量传递给我的控制器。我一直在试图找出一个没有成功的解决方案。任何人都可以引导我在正确的方向吗?如何将函数中的变量传递给我的控制器?
var myApp = angular.module('myApp', []);
myApp.controller('mainCtrl', ['$scope', function($scope){
$scope.videoUrl = videoUrl;
}])
// Helper function to display JavaScript value on HTML page.
function showResponse(response) {
var videoUrl = [];
for (prop in response.items) {
videoUrl[prop] = "https://www.youtube.com/embed/" + response.items[prop].snippet.resourceId.videoId;
}
}
// Called automatically when JavaScript client library is loaded.
function onClientLoad() {
gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
}
// Called automatically when YouTube API interface is loaded
function onYouTubeApiLoad() {
gapi.client.setApiKey('#######');
search();
}
function search() {
// Use the JavaScript client library to create a search.list() API call.
var request = gapi.client.youtube.playlistItems.list({
part: 'snippet',
playlistId: '########'
});
// Send the request to the API server,
// and invoke onSearchRepsonse() with the response.
request.execute(onSearchResponse);
}
// Called automatically with the response of the YouTube API request.
function onSearchResponse(response) {
showResponse(response);
}
[将变量传递给AngularJS控制器,最佳实践?](http://stackoverflow.com/questions/11703477/pass-variables-to-angularjs-controller-best-practice) –
你在哪里试图呼叫“showResponse”? – shivas
我正在使用谷歌apis客户端库,但我想你不能用角度来使用它。 – Grant