简体   繁体   English

数据绑定foreach一个可观察的数字

[英]data-bind foreach for an observable number

I am trying to display stars for each item in a list 我正在尝试为列表中的每个项目显示星星

I have an interface that has the 5-star score on a video, how do I do a foreach for the count of that score? 我有一个在视频上具有5星级得分的界面,如何对该得分进行计数? rather than creating an array for the score? 而不是为得分创建数组?

interface Video{
    Score: number;
}
<td>
    <span data-bind="foreach: score">
       <span class="glyphicon-star"></span>
    </span>
    <span data-bind="foreach: 5 - score">
       <span class="glyphicon-star-empty"></span>
    </span>
</td>

You could either use Array.from() or the Array(score) to create an array from the score 您可以使用Array.from()Array(score)从得分创建一个数组

Array.from({ length: score })

or 要么

[...Array(score)]

(Use score() if it's an observable ) (如果observable请使用score()

Here's a minimal snippet: 这是一个最小的片段:

 const model = { score: 3 } ko.applyBindings(model) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <span data-bind="foreach: Array.from({ length: score })"> <span class="glyphicon-star">*</span> </span> 

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

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