KnockoutJs v2.3.0 : Error You cannot apply bindings multiple times to the same element

后端 未结 13 1151
闹比i
闹比i 2020-12-04 17:38

I\'ve just upgraded to 2.3.0 and now I\'m getting the error

You cannot apply bindings multiple times to the same element.

that

13条回答
  •  温柔的废话
    2020-12-04 17:58

    Something that can happen as well which throws this exception is the following. Say you have:

    ko.applyBindings(myViewModel1, document.getElementById('element1'));
    ...
    ko.applyBindings(myViewModel2, document.getElementById('element2'));
    

    Now, when both #element1 and #element2 don't exist you'll get the error. The reason is that Knockout's applyBindings falls back on document.body as root element when #element1 and #element2 are not found. Now it tries to apply binding twice on the body...

    Not a nice fallback of Knockout if you ask me. I'd rather have a clear error message that the element does not exist in the DOM (yet).

    Hope this helps some people.

提交回复
热议问题