2016-06-10 77 views
0

代码:http://plnkr.co/edit/0eHQPVRHmCWelK6VEYVE?p=preview如何添加Dyamic JSON下拉菜单中角JS

这是从JSON文件的分析图表:http://happyshappy.13llama.com/wp-json/llama/v1/stats

我想提出一个下拉菜单,在图表的顶部,在下拉菜单中,JSON的“label”元素应该通过GET可见。 一旦用户选择一个标签,它的数据在图表中可见。

请尽快帮助我!

app.controller('MainCtrl', function($scope, $http) { 
$http.get('http://happyshappy.13llama.com/wp-json/llama/v1/stats').then(function(response) { 
var visitDates = response.data.visits.labels.map(function(dateString) { 
    return new Date(dateString); 
}); 

$scope.visits = response.data.visits.datasets; 
var dateData = []; 
var countData = [], index = 0 , i; 
angular.forEach($scope.visits, function(dataSet) { 
    dataSet.data = dataSet.data.map(function(count, i) { 
    index++; 
    dateData[i] = visitDates[i]; 
    countData[i] = count; 
    return { 
     date: visitDates[i], 
     count: count, 
     dateData: dateData, 
     countData: countData 
    }; 
    }); 
}); 
+0

http://happyshappy.13llama.com/wp-json/llama/v1/stats不工作,所以我们无法看到JSON – miquelarranz

+0

已修复!再次检查,并请尽快给我解决方案 –

回答

0

我不知道在所有关于你想要做什么,而是你想选择与标签和我的事情,如果你点击一个标签,然后在图表显示标签的值,右?

<select ng-options="item as item.label for item in visits" ng-click="changeValues()" ng-model="selected"></select> 

,然后创建功能在您控制器

$scope.changeValues() { 
    // You have the selected label in $scope.selected, so you have to iterate 
    // over the visits array and then set the data where label == $scope.selected in the chart values 
} 

我现在不能测试,但我希望这个想法可以帮助你解决这个问题。

+0

嘿!感谢回复。第一个代码正在工作,不知道如何处理第二个代码。 :( –

+0

看看这里:http://plnkr.co/edit/lcGPGIluRidtUr8V6mIm?p=preview –