我想在控制器函數的外部使用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