我正在使用Chart.js做点什么。我正在使用此函数($ scope.getAreas)来获取构建图表的信息。当我在"之外打印变量($ scope.AreasInteres)时,然后"函数返回" undefined"。
var app = angular.module('App', ["chart.js"]);
app.controller('chartsCtrl', function($http, $scope) {
var vt = this;
vt.chartPieLabels = ["Revistas", "Libros", "Internet", "Otros"];
vt.chartPieCharts = [152, 51, 68, 210];
vt.chartPieOptions = {
maintainAspectRatio: true,
responsive: true
};
vt.chartPieColours =['#494750', '#999999', '#cc3321', '#2fb467'];
$scope.AreasInteres;
$scope.getAreas = function(){
$http({
url: 'contarAreasInteres/',
method: 'GET'
}).then(function (response) {
$scope.AreasInteres = response.data;
console.log($scope.AreasInteres);
});
console.log($scope.AreasInteres);
};
$scope.getAreas();
});
我一直在使用相同的方法在同一个项目中从REST获取东西,但我没有遇到任何问题。 提前谢谢。
答案 0 :(得分:0)
.then
函数返回一个promise,这意味着它等待异步任务完成并在完成后执行它的回调函数。它仍在继续执行更多代码。
Angulars $http
服务返回一个promise,当对contarAreasInteres
的异步调用由于错误而完成或失败时,该promise将被解析。因此,.then
console.log
之外undefined
打印test = b'\x02\x00\x00\x01'
ref = b'\x02\x00\x00\x00'
是有意义的,因为尚未收到任何数据。
Here解释了为什么承诺很重要以及它们如何运作。