문제

If i have a div section with class container as below where i need to populate a template based on dropdown selected. How can i do that using knockout.js.

    <div class="container"></div>

    <!--Dropdown-->
    <select>
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
    </select>

    <!--Templates-->
    <script type="text/html" id="A-template">
      <span data-bind="text:Value"></span>
    </script>
    <script type="text/html" id="B-template">
      <span data-bind="text:Value"></span>
    </script>
    <script type="text/html" id="A-template">
      <span data-bind="text:Value"></span>
    </script>
도움이 되었습니까?

해결책

You can use a computed to calculate the template name:

this.SelectionTemplate = ko.computed(function() {
    return self.Value() + "-template";
});

Your template binding would look like this:

data-bind="template: { name: SelectionTemplate, data: $root }"

Here is a JSFiddle Sample: http://jsfiddle.net/6Shce/1/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top