Javascript Angular Chart.js无法使用$http调用
我正在使用填充饼图。为了填充图表,我需要在服务中调用Javascript Angular Chart.js无法使用$http调用,javascript,angularjs,charts,Javascript,Angularjs,Charts,我正在使用填充饼图。为了填充图表,我需要在服务中调用$http.get()并从控制器调用服务方法。但是如果我打$http电话,图表就不起作用了 angular.module('shopAdminApp')) .service('ChartService',['$http',ChartService]) 功能图表服务($http){ this.GetAllChartData=函数(月、年){ 返回$http.get('GetMonthlyStatistics?月='+月+'&年='+年+');
$http.get()
并从控制器调用服务方法。但是如果我打$http
电话,图表就不起作用了
angular.module('shopAdminApp'))
.service('ChartService',['$http',ChartService])
功能图表服务($http){
this.GetAllChartData=函数(月、年){
返回$http.get('GetMonthlyStatistics?月='+月+'&年='+年+');
}
}
angular.module('shopAdminApp')
.controller('chartCtrl',[“ChartService”,chartCtrl]);
功能ChartCtrl(ChartService){
//this.labels=[“下载销售”、“店内销售”、“邮购销售”];
//这个数据=[10,20,100];
var promise=ChartService.GetAllChartData(2012年12月12日);
承诺。然后(功能(响应){
console.log(response.data);
this.chartsdata=response.data;
this.labels=response.data.BranchNames;
this.data=response.data.branch每月总销售额;
});
//console.log(this.chartsdata);
}
试试这样的东西:
function ChartCtrl(ChartService) {
var self = this;
var promise = ChartService.GetAllChartData(12, 2012);
promise.then(function (response) {
self.chartsdata = response.data;
self.labels = response.data.BranchNames;
self.data = response.data.BranchMonthlyTotalSales;
});
}
你必须记住,
this
关键字指的是当前上下文,所以当你使用promise回调函数时,this
不再指向你的控制器,而是指向回调函数本身。但是你有GetMonthlyStatistics?month=
这不是一个url…它是一个url,就像我使用.net mvc和angular js一样。。response.data.BranchNames返回我为标签注释的精确数组,response.data.BranchMonthlyTotalSale返回我为数据注释的精确值。我使用console.log看到了它。因此没有问题。您确定服务器的响应正常吗?为什么不在promise的上使用拒绝处理函数作为第二个param函数呢。没问题。我在处理程序中检查了数据和标签的值,它们都正常。但图表并没有显示出来。