简体   繁体   English

Knockout.js HTML绑定不起作用

[英]knockoutjs html binding not working

I'm trying to use the html binding to bind some html to a label, causing the label to display the specified text. 我正在尝试使用html绑定将某些html绑定到标签,从而导致标签显示指定的文本。

The label does not display any text, but inspecting the page in the browser shows that it uses the html binding to display some text (without the text being displayed). 该标签不显示任何文本,但是在浏览器中检查页面显示该标签使用html绑定显示了一些文本(未显示文本)。

Why is the text not displayed in the label? 为什么文本未显示在标签中?

Here is my code: 这是我的代码:

Binding the html to viewmodel (take note of code comment): 将html绑定到viewmodel(注意代码注释):

"use strict";

qb.bindings.QuoteDetails = function (quoteDetailsVm) {
    var _$step1 = $("#step1");

    var _quoteDetailsSelector = ".quoteBuy";
    _$step1.find(".saveQuote" + _quoteDetailsSelector)
        .koClick("saveQuote")
        .koText("saveQuoteLabel");


    var ctaText = _$step1.find(".callToActionDescription");
    ctaText.koHtml("processedCtaText");
    $.koDataBind();

    quoteDetailsVm.email = "'sdfd'";
    _$step1.find("div.email label").koHtml(quoteDetailsVm.email); // this is the label to bind

    var _nodes = _$step1.find(_quoteDetailsSelector);
    _nodes.push(ctaText[0]);
    _.each(_nodes, function(node) {
        ko.applyBindings(quoteDetailsVm, node);
    });
};

This is where we define kohtml(): 这是我们定义kohtml()的地方:

(function ($) {
    var _bindings = [];

    var _koBind = function ($selector, binding, value) {
        var _jsToString = function(input) {
            var _result = "";

            for (var _key in input) {
                var _val = input[_key];
                if (typeof(_val) === "object")
                    _val = _jsToString(_val);

                _result = _result + (_result ? ", " : "") + _key + ": " + _val;
            }

            return "{ " + _result + " }";
        };

        if ($selector.length === 0) {
            console.warn("koBind selector does not return any elements - " + $selector.selector);
            return $selector;
        }

        if (typeof (value) === "object")
            value = _jsToString(value);

        _bindings.push({
            $selector: $selector,
            binding: binding,
            value: value
        });

        return $selector;
    };

    $.fn.koBind = function (binding, value) {
        return _koBind(this, binding, value);
    };

    $.fn.koVisible = function (visibility) {
        return _koBind(this, "visible", visibility);
    };

    $.fn.koText = function (text) {
        return _koBind(this, "text", text);
    };

    $.fn.koHtml = function (html) {
        return _koBind(this, "html", html);
    };

    $.fn.koCss = function (classes) {
        return _koBind(this, "css", classes);
    };

    $.fn.koStyle = function (styles) {
        return _koBind(this, "style", styles);
    };

    $.fn.koAttr = function (attrs) {
        return _koBind(this, "attr", attrs);
    };

    $.fn.koForeach = function (enumerable) {
        return _koBind(this, "foreach", enumerable);
    };

    $.fn.koIf = function (condition) {
        return _koBind(this, "if", condition);
    };

    $.fn.koIfnot = function (condition) {
        return _koBind(this, "ifnot", condition);
    };

    $.fn.koWith = function (context) {
        return _koBind(this, "with", context);
    };

    $.fn.koClick = function (callback) {
        return _koBind(this, "click", callback);
    };

    $.fn.koEvent = function (events) {
        return _koBind(this, "event", events);
    };

    $.fn.koSubmit = function (onSubmit) {
        return _koBind(this, "submit", onSubmit);
    };

    $.fn.koEnable = function (condition) {
        return _koBind(this, "enable", condition);
    };

    $.fn.koDisable = function (condition) {
        return _koBind(this, "disable", condition);
    };

    $.fn.koValue = function (value) {
        return _koBind(this, "value", value);
    };

    $.fn.koHasfocus = function (condition) {
        return _koBind(this, "hasfocus", condition);
    };

    $.fn.koChecked = function (condition) {
        return _koBind(this, "checked", condition);
    };

    $.fn.koOptions = function (optionList) {
        return _koBind(this, "options", optionList);
    };

    $.fn.koOptionsCaption = function (optionsCaption) {
        return _koBind(this, "optionsCaption", optionsCaption);
    };

    $.fn.koSelectedOptions = function (selectedList) {
        return _koBind(this, "selectedOptions", selectedList);
    };

    $.fn.koUniqueName = function (enabled) {
        return _koBind(this, "uniqueName", enabled);
    };

    $.fn.koPlaceholder = function(text) {
        return _koBind(this, "attr", { placeholder: text });
    };

    $.koDataBind = function () {
        var _elements = [];

        // put all the elements into an array
        var _findElement = function (el) {
            var _found = _elements.filter(function (element) {
                return element.element == el;
            });

            if (_found)
                return _found[0];

            return false;
        };

        $.each(_bindings, function (i, binding) {
            $.each(binding.$selector, function (j, el) {
                var _element = _findElement(el);
                if (_element) {
                    _element.bindings.push({
                        binding: binding.binding,
                        value: binding.value
                    });
                } else {
                    _elements.push({
                        element: el,
                        bindings: [{
                            binding: binding.binding,
                            value: binding.value
                        }]
                    });
                }
            });
        });

        _bindings = [];

        $.each(_elements, function (index, element) {
            var _el = element.element;

            var _bindVal = "";
            $.each(element.bindings, function (i, binding) {
                if (_bindVal)
                    _bindVal = _bindVal + ", " + binding.binding + ": " + binding.value;
                else
                    _bindVal = binding.binding + ": " + binding.value;
            });

            $(_el).attr("data-bind", _bindVal);
        });
    };

    $.koApplyBindings = function (viewModel) {
        $.koDataBind();
        ko.applyBindings(viewModel);
    };
})(jQuery);

This is the relevant html that has been coded: 这是已编码的相关html:

<!-- Primary Email Field-->

    <div class="email row">
        <div class="label">
            <label></label>
        </div>
        <div class="field">
            <input class="email" type="text" placeholder="e.g. john.smith@example.com" autocomplete="email" />
            <p class="error-message emailError"></p>
        </div>
    </div>

    <!-- End Primary Email Field-->

This is the output html by inspecting the page in the browser: 通过在浏览器中检查页面,这是输出html:

<div class="email row">
        <div class="label">
            <label data-bind="html: 'sdfd'"></label>
        </div>
        <div class="field">
            <input class="email" type="text" placeholder="e.g. john.smith@example.com" autocomplete="email">
            <p class="error-message emailError"></p>
        </div>
    </div>

What is wrong with the code in your opinion? 您认为代码有什么问题?

In Knockout, the HTML describes the entire app. 在淘汰赛中,HTML描述了整个应用程序。 In the same way that ordinary HTML describes a page's structure, the Knockout bindings describe all the behaviors: what is clickable, what displays a variable, etc. The technique of attaching bindings you have here removes all the clarity without making anything better. 与普通HTML描述页面结构的方式相同,Knockout绑定描述了所有行为:可单击的对象,显示变量的对象等。这里使用的绑定绑定技术消除了所有的清晰度,而没有任何改善。 There's an extra layer of code just to attach bindings that requires a deep understanding of the HTML structure just to know what's being bound where. 还有一层附加的代码用来附加绑定 ,这需要对HTML结构有深入的了解,才知道绑定在什么地方。 It's inscrutable. 难以理解。 And it evidently doesn't work. 而且显然不起作用。

My recommendation is to put the markup back into the HTML, at least for the element in question, and see whether that works. 我的建议是将标记放回HTML中,至少对有问题的元素放回HTML中,然后看是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM