问题在于 ckeditor 将其替换为 contenteditable 并且它不知道任何值绑定。您可以编写一个 customBindingHandler 来执行此操作。不久前我为 CLEditor 写了一个。这是它的样子
ko.bindingHandlers.cleditor = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var modelValue = valueAccessor();
var options = {}
if (allBindingsAccessor().cleditorOptions)
options = $.extend(options, allBindingsAccessor().cleditorOptions);
var ko_editor = $(element).cleditor(options);
if (allBindingsAccessor().cleditorCSS) {
var cssLink = ko_editor[0].doc.createElement("link");
cssLink.rel = "stylesheet";
cssLink.type = "text/css";
cssLink.href = allBindingsAccessor().cleditorCSS;
if (ko_editor[0].doc.head === undefined) {
var headElement = ko_editor[0].doc.getElementsByTagName('head');
headElement[0].appendChild(cssLink);
}
else
ko_editor[0].doc.head.appendChild(cssLink);
}
ko_editor[0].change(
function (a) {
var elementValue = ko_editor[0].doc.body.innerHTML;
if (ko.isWriteableObservable(modelValue)) {
modelValue(elementValue);
}
else { //handle non-observable one-way binding
var allBindings = allBindingsAccessor();
if (allBindings['_ko_property_writers'] && allBindings['_ko_property_writers'].htmlValue) allBindings['_ko_property_writers'].htmlValue(elementValue);
}
}
);
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()) || "";
var ko_editor = $(element).cleditor();
if (ko_editor[0].doc.body.innerHTML !== value) {
ko_editor[0].doc.body.innerHTML = value;
ko_editor[0].focus();
}
}
};
html看起来像这样
<textarea rows="20" data-bind="cleditor: Body, cleditorCSS: 'path/ComposeEditor.css', cleditorOptions:$root.EditorOptions"></textarea>
因此您现在可以提供一个名为 Body 的可观察对象(存储值的位置)、一个 css 和在您的视图模型中指定的选项。
希望对您有所帮助。