【问题标题】:Value binding in <script>-tag not working<script>-tag 中的值绑定不起作用
【发布时间】:2018-11-18 10:24:24
【问题描述】:

有没有办法使用DotVVM&lt;script&gt;-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>

&lt;!-- ko text: ChartLabels --&gt;&lt;!-- /ko --&gt;, 行我收到此错误

Uncaught SyntaxError: Unexpected token :

有人知道如何使用正确的值绑定(这里:在&lt;script&gt;-tag 中将其显示为文本)?

【问题讨论】:

    标签: javascript html asp.net data-binding dotvvm


    【解决方案1】:

    值绑定在 script 元素中不起作用 - 它们被转换为 Knockout JS 表达式(您在代码 sn-p 中拥有的 HTML cmets)。

    你有两个选择:

    1. 使用资源绑定:{{resource: ChartLabels}} 您需要进行所有转换,以便生成有效的 JavaScript 表达式 - 它只是在输出 HTML 中呈现一个字符串。

    2. 您可以使用 JavaScript 直接从视图模型中获取值:

    var vm = dotvvm.viewModels.root.viewModel;
    
    // you need to use () to read the value of Knockout observables 
    var chartLabels = vm.ChartLabels();   
    

    客户端的视图模型封装在 Knockout observables 中,因此请确保不要忘记 () 来访问该值。

    【讨论】:

    • 感谢您的回答!当我尝试使用第二个选项时,我在var vm = dotvvm.viewModels.root.viewModel; 行收到错误Uncaught ReferenceError: dotvvm is not defined。您需要更多信息吗?
    • 第一个选项有效,只有当我没有在 MasterPage 中使用 SpaContentPlaceHolder 时。这是为什么呢?
    • 关于第二个选项,看起来代码是在dotvvm.js初始化之前执行的。将脚本放在&lt;dot:InlineScript Dependencies="dotvvm"&gt; 中 - 它会在加载 DotVVM 后加载脚本,然后您应该将脚本包装在此: dotvvm.events.init.subscribe(function () { // 将您的代码放在这里 });它将使脚本在 DotVVM 初始化后运行。它还应该解决 SPA 页面中的问题 - 页面中直接包含的脚本也存在问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2016-09-27
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多