简体   繁体   English

敲除绑定迭代

[英]Knockout binding iteration

I am having a scenario where based on the Observable flag I need to iterate ABC and XYZ 我有一种情况,其中需要基于Observable标志来迭代ABC和XYZ

Current Code 当前代码

<div  data-bind="visible: isEnabled">
           <ul data-bind="foreach: relatedObservalableArray">
               <li>
               <!-- Hundred lines of code>
               </li>
            </ul>
</div>


<div data-bind="visible: !isEnabled">
        <ul data-bind="foreach: unRelatedObservalableArray">
               <li>
                <!-- Same Hundred lines of code>
               </li>
            </ul>
   </div>

I am feeling code duplicate. 我感觉代码重复。

Is any way I can group the two html functions to a single one? 有什么办法可以将两个html函数分组为一个函数?

I want to change only in HTML part due to some other ... 由于其他一些原因,我只想更改HTML部分...

I am new to Knockout. 我是淘汰赛的新手。 Can someone help me? 有人能帮我吗?

Sure you can use the knockout template binding for this. 当然,您可以为此使用敲除template绑定。 Read it more here . 在这里阅读更多。

So for the changes it would be something like below but you can do variations basing on what you read from the link I gave. 因此,对于更改而言,如下所示,但是您可以根据您从我给出的链接中读取的内容进行更改。

<script type="text/html" id="template-name">
   Hundred lines of code...
</script>

<div  data-bind="visible: isEnabled">
  <ul data-bind="foreach: relatedObservalableArray">
    <li data-bind="template: 'template-name'">
    </li>
  </ul>
</div>


<div data-bind="visible: !isEnabled">
  <ul data-bind="foreach: unRelatedObservalableArray">
     <li data-bind="template: 'template-name'">
     </li>
  </ul>
</div>

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

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