我正在尝试将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);
}
它可能会更好/更容易,如果你能得到这个东西成的角,所以它的服务中的所有发生。这就是应该以角度进行数据共享的方式。但是,由于的性质,这可能具有挑战性onClientLoad
。肮脏的方法是:
直接获取控制器的作用域并将其设置在该作用域上。假设您已经定义了以下内容:
<div ng-controller="mainCtrl"></div>
您可以使用jQuery获取该控制器的作用域:
function showResponse(response) {
var videoUrl = [];
for (prop in response.items) {
videoUrl[prop] = "https://www.youtube.com/embed/" + response.items[prop].snippet.resourceId.videoId;
}
var scope = $('[ng-controller="mainCtrl"]').scope();
scope.videoUrl = videoUrl;
}
请注意,这将导致有角度的纯粹主义者哭泣并咬牙。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句