我有一个页面上显示的步骤列表。某些步骤(但不是所有)将具有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());