我是AngularJS的新手,我卡住了:\ NEED HELP,please!
下面的代碼的目的是從數據庫中獲取一個特定的數據(一個整數),以建立一個圓環圖。 我運行函數dataforGraphs()以獲取該值。但是,在功能之外,我「放棄」了這個價值。
(代碼時,我提出的意見,以更好地解釋情況)
uONEControllers.controller('HomeCtrl', ['$scope', 'GraphService',
function ($scope, GraphService) {
var ammountOfFinishedOrders = {};
dataforGraphs(); // calling the function
function dataforGraphs() {
GraphService.getAmmountOfFinishedOrders()
.success(function (data) {
$scope.ammountOfFinishedOrders = data.GetAmmountOfFinishedOrdersResult;
//this console.log shows me the value 3. Excellent!
console.log("value 1:", $scope.ammountOfFinishedOrders)
})
.error(function (error) {
$scope.status = 'Unable to load data:' + error.message;
});
//HOWEVER... here is undefined
console.log("value 2:", $scope.ammountOfFinishedOrders)
};
//here, outside the function, too :(
console.log("value 3:", $scope.ammountOfFinishedOrders)
//building the doughnut graphic
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
//THE PROBLEM IS HERE. The $scope.ammountOfFinishedOrders is undefined! WHY??
$scope.data = [
$scope.ammountOfFinishedOrders, 4, 1, 3, 2, 1, 4
];
}]);
我已經嘗試返回$ scope.ammountOfFinishedOrders,但仍然一無所獲。這可能是範圍繼承問題嗎?如果是的話,我應該怎麼做才能解決這個問題?如果沒有...好吧,無論如何幫助我:D
非常感謝!
哈哈,你通過1:40秒打我:P – squiroid
哈哈哈哈。每個人都在線。 :P – nikhil
謝謝!有效!我現在可以停下來對着電腦碰頭了:D我沒有意識到異步效應。 再次感謝。 –