我正在尝试通过observable而不是数组来复制图像元素(表示正确/错误的答案)。可以这样做吗? (请参阅XXXXXXXXX中标记的位置)。 answerCorrect和answerfailed在其他地方更新。
JS:
var singlePhase=function (phaseNo,questionsTotal)
{
var self=this;
self.phaseNo=phaseNo;
self.answersTotal=questionsTotal;
self.answerCorrect=ko.observable(0);
self.answerfailed=ko.observable(0);
}
var QuestionViewModel = function () {
var self = this;
var tmpArrPhases = new Array();
tmpArrPhases.push(new singlePhase(1,2));
tmpArrPhases.push(new singlePhase(2,2));
tmpArrPhases.push(new singlePhase(3,3));
self.allPhases(tmpArrPhases);
}
我希望在HTML中使用:
<div data-bind="foreach: allPhases">
<div class="progress_bar_bottom_mustry" data-bind="foreach:XXXXXXXXX">
<img src="images/correct.png" data-bind="visible:true">
<img src="images/wrong.png" data-bind="visible:true">
</div>
</div>
感谢!
答案 0 :(得分:1)
您可以在foreach
绑定中创建一个空数组:
<div data-bind="foreach: allPhases">
<div class="progress_bar_bottom_mustry" data-bind="foreach:new Array(YourObservalbe())">
<img src="images/correct.png" data-bind="visible:true">
<img src="images/wrong.png" data-bind="visible:true">
</div>
</div>
这是一个例子: http://jsfiddle.net/7UDfM/