【发布时间】:2018-11-18 10:24:24
【问题描述】:
有没有办法使用DotVVM 在<script>-tags 中使用值绑定?
我想将 ViewModel 的属性显示为文本。这里我想使用chartjs设置图表的标签和数据。
这就是我目前得到的:
我正在尝试使用 chartjs 制作图表。
ViewModel:(变量在PreRender() 方法中填充数据)
private List<int> chartLabels = new List<int>();
private List<int> chartData1 = new List<int>();
public string ChartLabels
{
get { return JsonConvert.SerializeObject(chartLabels); }
}
public string ChartData1
{
get { return JsonConvert.SerializeObject(chartData1); }
}
dothtml 文件:
<canvas id="myChart" style="padding: 0;margin: auto;display: block; "> </canvas>
<script>
var dataT = {
labels: {{value: ChartLabels}},
datasets: [{
label: "Africa",
data: {{value: ChartData1}},
fill: false,
backgroundColor: "rgba(255, 0, 0, 0.8)",
borderColor: "rgba(255, 0, 0, 0.8)",
borderWidth: 1
}]
};
var ctx = $("#myChart").get(0).getContext("2d");
var myNewChart = new Chart(ctx, {
type: 'line',
data: dataT,
options: {
responsive: true,
title: { display: true, text: 'World population per region (in millions)' },
legend: { position: 'bottom' },
scales: {
xAxes: [{ gridLines: { display: false }, display: true, scaleLabel: { display: false, labelString: '' } }],
yAxes: [{ gridLines: { display: false }, display: true, scaleLabel: { display: false, labelString: '' }, ticks: { stepSize: 250, beginAtZero: true } }]
},
}
});
</script>
把所有这些放在一起,这就是我打开页面时得到的:
<script>
var dataT = {
labels: <!-- ko text: ChartLabels --><!-- /ko -->,
datasets: [{
label: "Africa",
data: <!-- ko text: ChartData1 --><!-- /ko -->,
fill: false,
backgroundColor: "rgba(255, 0, 0, 0.8)",
borderColor: "rgba(255, 0, 0, 0.8)",
borderWidth: 1
}]
};
var ctx = $("#myChart").get(0).getContext("2d");
var myNewChart = new Chart(ctx, {
type: 'line',
data: dataT,
options: {
responsive: true,
title: { display: true, text: 'World population per region (in millions)' },
legend: { position: 'bottom' },
scales: {
xAxes: [{ gridLines: { display: false }, display: true, scaleLabel: { display: false, labelString: '' } }],
yAxes: [{ gridLines: { display: false }, display: true, scaleLabel: { display: false, labelString: '' }, ticks: { stepSize: 250, beginAtZero: true } }]
},
}
});
</script>
在<!-- ko text: ChartLabels --><!-- /ko -->, 行我收到此错误
Uncaught SyntaxError: Unexpected token :
有人知道如何使用正确的值绑定(这里:在<script>-tag 中将其显示为文本)?
【问题讨论】:
标签: javascript html asp.net data-binding dotvvm