2016-06-10 74 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 –