如何将HTML添加到此Knockout.js Viewmodel变量?

时间:2015-06-16 21:16:47

标签: javascript html knockout.js knockout-components knockout-templating

我有一个页面上显示的步骤列表。某些步骤(但不是所有)将具有1-3个子步骤。我认为最简单的方法是在viewmodel中的每个步骤声明中手动编写HTML。

如果您查看下面的代码,我想要做的是:

this.sidebarStepModel1 = new SidebarStepModel();
this.sidebarStepModel1.message("step 2 <ul><li>substep 1</li><li>substep2</li></ul>");

问题是,写在那里的HTML被解释为string而不是HTML。我怎样才能将HTML解释为实际的HTML?

小提琴:https://jsfiddle.net/8o31m6rq/2/

HTML:

<ul>
    <sidebar-step params="vm: sidebarStepModel0"></sidebar-step>
    <sidebar-step params="vm: sidebarStepModel1"></sidebar-step>
    <sidebar-step params="vm: sidebarStepModel2"></sidebar-step>
</ul>

的JavaScript /敲除:

//custom element <sidebar-step>
ko.components.register("sidebar-step", {
    synchronous: true,
    viewModel: function (params) {
        this.vm = params.vm;
    },

    template: "<li data-bind='text: vm.message'>vm.onChangeElement</li>"
});

// model
var SidebarStepModel = function () {
    this.message = ko.observable("step description");
};

// viewmodel
var OrderGuideViewModel = function () {

    this.sidebarStepModel0 = new SidebarStepModel();
    this.sidebarStepModel0.message("step 1");

    this.sidebarStepModel1 = new SidebarStepModel();
    this.sidebarStepModel1.message("step 2");

    this.sidebarStepModel2 = new SidebarStepModel();
    this.sidebarStepModel2.message("step 3");
};

ko.applyBindings(new OrderGuideViewModel());

1 个答案:

答案 0 :(得分:1)

您只需使用html binding代替object绑定:

remove()

Updated fiddle