0

我想做一个弹出框(引导程序)自定义绑定。

我已经这样定义了:

    ko.bindingHandlers.popover = {
        update: function (element, valueAccessor)
        {
            var template = ko.unwrap(valueAccessor);

            $(element).popover({
                placement: 'top',
                html: true,
                content: 'text!' + template() <---- How can i get html into here?
            });
        }
    };

   <button data-bind="popover: 'templates/mytemplate.html'">
      PopOver
   </button>

问题是,我不确定如何注入我想要的 html。自然我想要一个模板路径来解决,但使用需要文本!插件没有我希望的那么好。

我怀疑我忽略了一些更简单的东西?

4

2 回答 2

1

只需向您的模板发出 ajax 请求

$.ajax(template).done(function (templateData) {
    $(element).popover({
        placement: 'top',
        html: true,
        content: templateData
        });
 });
于 2015-03-10T06:30:49.153 回答
0

现在你的模板是文字字符串'templates/mytemplate.html'。这可能不是您想要的,您想要该文件中的文本。使用fs.readFileSync之类的东西从该文件中同步获取文本。

如果您打算将 html 的路径传递到绑定中,您可能需要将变量名称从模板更改为路径。

于 2015-03-09T18:12:52.403 回答