我想在控制器函数的外部使用single_video变量。它在第一个控制台日志中打印良好。但是它给出了single_video是第二个console.log中的未定义错误,它在控制器函数之外。我对javascript和angularjs都很穷。Javascript定义全局变量
var single_video;
var app = angular.module('myApp', []);
app.controller('randomVideo', function($scope, $http) {
var onSuccess = function(response){
$scope.video = response.data;
single_video = $scope.video;
//First console.log
console.log('1st ID='+single_video.yt_id);
};
var onError = function(reason){
$scope.error = "There is an error about getting random_video.php";
};
$http.get("http://www.ytmdb.com/ytvideo/api/random_video.php")
.then(onSuccess, onError);
});
//Second console.log
console.log('2nd ID='+single_video.yt_id);
它如何在第二个控制台中打印,除非它有价值? – azad