我在淘汰赛中有一个模型,有这样的东西:
var Model = function(model)
{
var self = root = this;
self.ObjectA = new ObjectA(model.ObjA);
self.includeImage = function(image){
self.ObjectA.Images.push(image);
}
}
var ObjectA = function(objA){
var self = this;
self.Images = ko.observableArray();
self.ObjectB = ko.observableArray(ko.utils.arrayMap(objA.ObjectsB, function(ob){
return new ObjectB(ob);
}));
}
var ObjectB = function(objB){
var self = this;
self.ObjectC = new ObjectC(objB.objC);
}
var ObjectC = function(objC){
var self = this;
self.ImagesC = ko.utils.arrayMap(objC.ImagesC, function(img){
return new Image(img);
});
}
var Image = function(image){
var self = this;
self.Key = image.Key;
self.Value = image.Value;
self.Url = image.Url;
}
然后,我将这个HTML放在桌子上:
<tbody data-bind="foreach: ObjectA.ObjectB">
<tr>
<td>
<!-- ko if: ObjectC.Images.length > 0 -->
<div>
<img data-bind="attr: { src: ObjectC.Images[0].Url }" />
<button data-bind="click: root.includeImage">include</button>
</div>
<div data-bind="foreach: ObjectC.Images">
<!-- ko if: $index() !== 0 -->
<div>
<img data-bind="attr: { src: Url }" />
<button data-bind="click: root.includeImage">include</button>
</div>
<!-- /ko -->
</div>
<!-- /ko -->
</td>
</tr>
</tbody>
我遇到的问题是函数includeImage,我收到的参数不是ObjectC的图像..参数类型为ObjectB ..为什么? 它的一些foreach坏了?
答案 0 :(得分:0)
我刚刚发现了这个问题!是一个愚蠢的问题,在问题的代码中,我忘了包括提出问题的东西! LOL
我编辑了代码,以便您可以看到主要问题,并且是第二个foreach之外的第一个按钮!当然,第一个foreach的root.includeImage不起作用。 我只是解决了问题:
<button class="action add" data-bind="click: function() { root.includeImage(ObjectC.Images[0]) }">include</button>