您的问题是如何使用字符串作为模板引擎的源代码?如果是这样,我已经为我的一个绑定做到了
https://github.com/AndersMalmgren/Knockout.Bindings/blob/master/src/knockout.bindings.js
我的仓库中的相关代码
var stringTemplateSource = function (template) {
this.template = template;
};
stringTemplateSource.prototype.text = function () {
return this.template;
};
var stringTemplateEngine = new ko.nativeTemplateEngine();
stringTemplateEngine.makeTemplateSource = function (template) {
return new stringTemplateSource(template);
};
并使用它
ko.bindingHandlers.myCustomBinding = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
ko.renderTemplate('<span data-bind="text: $data"></span>', bindingContext.createChildContext(valueAccessor()), { templateEngine: stringTemplateEngine }, element, "replaceChildren");
return { controlsDescendantBindings: true };
}