在angular中使用json数组整数
using a json array integer in angular
我想在一个范围内导入一个json数组的整数。数据用于填充d3条,但当它不起作用时!它将数组置于作用域中。因为当我在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();
})
相关文章:
- 需要帮助设置json数组
- 如何使用 node.js 比较两个 json 数组
- 根据id将json数组组合为一个json数组
- 如何创建JSON数组
- 如何将package.json数组传递给grunt.js
- 访问json数组中的对象
- JSON数组数据返回Undefined
- 如何向JSON数组动态添加属性
- 如何从json数组中获取特定值
- 从多维嵌套json数组创建下拉列表
- 在ListView中添加JSON数组中存储的图像-ReactNative
- jquery在json数组中循环
- 将 JSON 数组解析为 JavaScript 数组
- 如何在php中读取没有任何键的JSON数组
- 如何在Jquery中迭代JSON数组
- 如何在cycle js中从JSON数组创建组件
- 如何使用jquery返回php-json数组对象
- JSON数组转换为JS对象数组
- 使用System从C#集合创建JSON数组.网状物剧本序列化
- 使用Underscore.js修改json数组中所选元素的更有效方法