Search code examples
knockout.jsjquery-templates

Template not being found when injected dynamically


The HTML below is injected into my site on an ajax call, but the template is not being picked up. Is there something I need to do to register it?

Unable to parse binding attribute. Message:
ReferenceError: answerTmpl is not defined;
Attribute value: template: { name: answerTmpl, foreach: answers }

Dynamic HTML:

<script id="answerTmpl" type="text/html">
    <div>
      <span></span>  <input type="checkbox" name="Answer" />
    </div>
</script>

<div id="answers" data-bind="template: { name: answerTmpl, foreach: answers }"></div>

Corresponding JS:

ko.applyBindingsToNode(document.getElementById('answers'));

Solution

  • You need to put your template name in quotes, so name: 'answerTmpl'