[英]AngularJS ng-repeat: Dynamically render/bind canvas inside loop
Relatively new to the world of AngularJS, enjoying it so far. 到目前为止,它对AngularJS来说还比较陌生。
However, I'm struggling with my attempt to loop through entries in my db and render a <canvas>
for each one. 但是,我一直在尝试遍历数据库中的条目并为每个条目渲染一个
<canvas>
。
Say this is my data (shortened for brevity): 说这是我的数据(为简洁起见,简称):
var paintings = [
{ "_id" : ObjectId("31c75"), "data" : "0,0,0,0" },
{ "_id" : ObjectId("deadb"), "data" : "1,3,0,255" }
];
Which is loaded into the controller by a factory: 由工厂装载到控制器中:
app.factory('paintings', ['$http', function($http) {
var o = {
paintings: []
};
o.getAll = function() {
return $http.get('/paintings')
.success(function(data) {
angular.copy(data, o.paintings);
});
}
return o;
}]);
I'm wanting to loop through each entry and construct a <canvas>
element, then pass that <canvas>
element to another object ( Grid
) with data
as an argument, which creates context and draws on that <canvas>
based on the data. 我想遍历每个条目并构造一个
<canvas>
元素,然后将该<canvas>
元素传递给另一个对象( Grid
),并以data
作为参数,这将创建上下文并基于数据绘制该<canvas>
。
Simple, right? 简单吧? Unfortunately, I'm at a loss for how to do so and do not have the language with which to ask a more poignant question.
不幸的是,我不知道该怎么做,也没有一种语言可以问一个更棘手的问题。
I think problems exist in the fact that I am using inline-templates which aren't yet rendered. 我认为问题在于我正在使用尚未渲染的内联模板。
This is generally the approach I am currently trying: 通常,这是我目前正在尝试的方法:
HTML: HTML:
<div ng-repeat="painting in paintings" some-directive-maybe="bindCanvas(painting._id)">
<canvas id="{{ painting._id }}" width="800" height="400"></canvas>
</div>
JS: JS:
app.controller('PaintingCtrl', [
'$scope',
function($scope) {
$scope.bindCanvas(canvasId) {
var grid = new Grid(document.getElementById(canvasId));
// Have fun with grid
}
}
]);
Help me, StackOverflow. 帮帮我,StackOverflow。 You're my only hope...
你是我唯一的希望...
var paintings = [
{ "_id" : ObjectId("31c75"), "data" : "0,0,0,0" },
{ "_id" : ObjectId("deadb"), "data" : "1,3,0,255" }
];
paintings should be in an array
. 绘画应该
array
。
app.controller('PaintingCtrl', [
'$scope',
function($scope) {
$scope.bindCanvas(canvasId) {
var grid = new Grid(document.getElementById(canvasId));
// Have fun with grid
}
//paintings should be on scope for ng-repeat to find it.
// If ng-repeat does not find paintings on scope then it will create a new empty paintings object on scope
$scope.paintings = [
{ _id : "31c75", data : "0,0,0,0" },
{ _id : "deadb", data : "1,3,0,255" }
];
}
]);
Update: 更新:
I have created 2 plunkers. 我创建了2个插件。
First, plunker just creates canvas
elements with static
width
and height
. 首先, punker仅创建具有
static
width
和height
canvas
元素。 Number of canvas
elements created is based upon number of paintings in painting.json
file. 创建的
canvas
元素数量取决于painting.json
文件中的painting.json
数量。
Second, plunker goes a step further and creates canvas
elements with dynamic
width
and height
. 其次, punker更进一步,创建具有
dynamic
width
和height
canvas
元素。 Number of canvas
elements created is based upon number of paintings in painting.json
file. 创建的
canvas
元素数量取决于painting.json
文件中的painting.json
数量。 Here width
and height
are based upon data
property in paintings.json
file. 这里的
width
和height
是基于paintings.json
文件中的data
属性的。
Hope this helps. 希望这可以帮助。
Following code also works for repeat chart on the same page. 以下代码也适用于同一页面上的重复图表。
<div ng-repeat="a in abc">
<canvas id="pieChart{{a}}" ng-bind = "bindCanvas(a)" ></canvas>
<div>
Add below code in JS file 在JS文件中添加以下代码
$scope.abc = [1,2,3];
$scope.bindCanvas = function(i) {
var ctx = document.getElementById("pieChart"+i);
new Chart(ctx,{
type: 'pie',
data: {
labels: ["Tele-conference", "Projector", "Laptop", "Desktop", "Coffee-machine"],
datasets: [{
label: "Population (millions)",
backgroundColor: ["red", "green","blue","violet","yellow"],
data: [200,100,300,400,150]
}]
},
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.