[英]using a json array integer in angular
我想在scope.data中导入一个整数的json数组,以用于d3条形图的粘贴,但是当它不起作用时! 它会在scope.data中获取数组,因为当我在html中执行{{data}}时,它会显示数组,但无法将其用于d3条,请帮忙!
angular.module('myApp', [])
//.controller('SalesController', ['$scope', function($scope, $http){
.controller('SalesController', function($scope, $http) {
/* $http.get('js/todos2.json')
.then(function(res){
$scope.data = res.data;
});*/
/*$http({method: 'GET', url: 'js/todos2.json'}).success(function(data)
{
$scope.data = data; // response data
});*/
$http.get('js/todos2.json').success(function(data) {
$scope.data = data;
});
/*$scope.data = [Math.random(),12,6,8,15];*/
/*$scope.data = 7*/
$scope.text = ('klopm');
//parseInt(data)
}).directive('bars', function ($parse) {
return {
restrict: 'E',
replace: true,
template: '<div id="chart"></div>',
scope:{data: '=data'},
link: function (scope, element, attrs) {
var data = scope.data;
function drawBars() {
var chart = d3.select('#chart')
.append("div").attr("class", "chart")
.selectAll('div')
.data(data[0]).enter()
.append("div")
.transition().ease("elastic") //had la ligne bantli zayda na9ssa b3da pr l'affichage il faut savoir a quoi sert
.style("width", function(d) { return d + "%"; })
.text(function(d) { return d + "%"; });
}
drawBars();
}
};
});
由于您是从json文件中获取数据的,因此不会立即填充$scope.data
,因此需要在绘制图表之前对其进行监视,以确保已填充该数据。 在指令定义中,您需要确保有数据,而不仅仅是立即调用drawBars()
函数。 所以像这样
scope.$watch('data', function(){
if(!scope.data) return; //If there is no data, do nothing
//now call your draw function
drawBars();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.