我正在使用带有角度的Morris图表来显示图形报告,其中数据通过其余API调用从我们的后端服务器中获取。重新加载角度指令以在API调用之后重绘morris.js图表
我能够在控制台日志中看到检索到的数据,但它不显示在图表中。我发现指令条形图在api调用之前被加载,因此显示$ scope.myModel中可用的数据。
我试图找到是否有某种角度的方式,可以帮助我重新加载指令时,从API调用收到数据。有人可以帮助我吗?
条形图从代码生成:
这里是我的代码
var sampleApp = angular.module('sample',[]);
sampleApp.directive('barchart', function() {
return {
// required to make it work as an element
restrict: 'E',
template: '<div></div>',
replace: true,
// observe and manipulate the DOM
link: function($scope, element, attrs) {
var data = $scope[attrs.data],
xkey = $scope[attrs.xkey],
ykeys= $scope[attrs.ykeys],
labels= $scope[attrs.labels];
Morris.Bar({
element: element,
data: data,
xkey: xkey,
ykeys: ykeys,
labels: labels
});
}
};
});
sampleApp.controller('sampleController',function($scope, $http){
$scope.values = []
$scope.xkey = 'range';
$scope.ykeys = ['total_tasks', 'total_overdue'];
$scope.labels = ['Total Tasks', 'Out of Budget Tasks'];
$http.get('http://api.*******.com/api/getAppID/?parameter=whatsapp').success(function(res) {
if(!res.error) {
if(res.status==1) res.status=true
else res.status=false
$scope.values[0] = res.metrices.total_shares
$scope.values[1] = res.metrices.unique_share_count
$scope.values[2] = res.metrices.total_clicks
$scope.values[3] = res.metrices.total_downloads
}
})
$scope.myModel = [
{ range: 'January', total_tasks: $scope.values[0], total_overdue: 5 },
{ range: 'January', total_tasks: $scope.values[1], total_overdue: 8 },
{ range: 'January', total_tasks: $scope.values[2], total_overdue: 1 },
{ range: 'January', total_tasks: $scope.values[3], total_overdue: 6 }
];
});
HTML部分:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.oesmith.co.uk/morris-0.4.3.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.4.3.min.js"></script>
<script src="js/sample.js"></script>
<meta charset=utf-8 />
</head>
<body ng-app="sample" ng-controller="sampleController">
<barchart xkey="xkey" ykeys="ykeys" labels="labels" data="myModel"></barchart>
</body>
你可以添加在http://plnkr.co/,这样我们可以很容易地调试 – 2014-09-05 11:43:55