獲取數據時,我有這非常惱人的問題,我覺得我有一個想法,爲什麼它引起的,但不能爲我的生命得到固定。也許是因爲我不知道編程的基本規則....表不填充從AJAX調用
因此,讓我演示一下這個問題:
HTML:
<!DOCTYPE html>
<html lang="en" ng-app="angularApp">
<head>
<meta charset="utf-8">
<link href="/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<section>
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Wall</h3>
</div>
<div class="panel-body">
<div ng-controller="wallPosts">
<div class="panel panel-default post" ng-repeat="post in posts">
<div class="panel-body">
<div class="row">
<div class="col-sm-3">
<canvas id="pie" class="chart chart-pie" chart-data="data" chart-labels="labels" chart-options="options">
</canvas>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div><!-- col-md-8 end -->
</div>
</div>
</section>
<!-- Bootstrap core JavaScript-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="/jsLib/bootstrap.js"></script>
<script src="/jsLib/angular.min.js"></script>
<script src="/angular/angularApp.js"></script>
<script src="/jsLib/chart.js/dist/Chart.min.js"></script>
<script src="/jsLib/angular-chart.js/dist/angular-chart.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
</script>
</body>
</html>
角控制器:
var angularApp = angular.module('angularApp', ['chart.js']);
angularApp.controller('wallPosts', ['$scope', '$http', function($scope, $http){
$scope.posts = [];
$scope.labels = ["Agree", "Disagree"];
$scope.data = [];
$http.get('/api/posts')
.success(function(res){
$scope.posts = res.posts;
//$scope.data = [100, 500];
$scope.data = [res.posts.agreeCounter, res.posts.disagreeCounter];
})
.error(function(data, status){
console.log(data);
});
}]);
API響應如下所示:
"posts": [
{
"postID": "postID1233",
"title": "testTitle",
"description": "testDescription",
"agreeCounter": 100,
"disagreeCounter": 50
}]
的問題是,「畫布」似乎正在產生右遠沒有我經過數據。當我將$ scope.data作爲[100,50](靜態)傳遞時,它可以工作。
順便說一句,在res.posts工程,我已經測試與post.title,post.description等(它也有post.agreeCounter),所以這不是問題。
那我怎麼去整理了這一點?任何輸入都會有幫助。再次
感謝, 傻眼
你能PLZ添加JSON從正在添加的服務器? typeof res.posts.agreeCounter'打印了什麼類型? –
數據返回爲空。 res.posts的作品,因爲我已經使用res.title,res.description但是當我使用它的畫布,它不起作用。 無論如何,我已經更新了原來的職位與響應。感謝您的光臨 –
如果數據爲'null',您如何期待您的圖表工作? :) –