只渲染两个连续绑定元素中的第一个

时间:2013-03-06 15:13:29

标签: knockout.js typescript

我不明白为什么这里的第二项没有呈现,即只有一个p得到输出..:

   <div class="row" data-bind="foreach: Foos">
       <div class="three columns">
           <p data-bind="text: CriteriaStub"></p>
           <p data-bind="text: CriteriaStub"></p>
       </div>
       <div class="three columns">
           <!--this gets rendered fine-->
           <p data-bind="text: Baa"></p>
       </div>
    </div>

似乎只会出现连贯的约束因素。

查看foreach的模型项:

var Foo= (function () {
    function Foo(jsonObject) {
        var self = this;

        self.AdHocRecipients = ko.observableArray(jsonObject.AdHocRecipients);
        self.CriteriaStub = ko.computed(function () {

            return "No criteria";
        });

        self.AdHocRecipientsToStringStub = ko.computed(function () {
            var stub = "";
            for(var i in self.AdHocRecipients()) {
                stub += (self.AdHocRecipients()[i].User) + ', ';
            }
            return self.AdHocRecipients().length > 0 ? stub.slice(0, -2) : "No recipiants";
        });
        var t = 2;
    }
    return Foo;
})();

父:

var ViewModel = (function () {
    function ViewModel(json) {

        var parsedFoos = new Array();
        for(var i in json.Foos) {
            parsedFoos.push(new Foo(json.Foos[i]));
        }
        this.Foos = ko.observableArray(parsedFoos);
    return ViewModel;
})();

1 个答案:

答案 0 :(得分:0)

我当然不确定,但唯一突出的可能是错误的是Foo是一个自我执行的功能,它也会自行返回。当你多次调用它时,我看不到你得到了什么。您是否尝试过将Foo作为一种功能?

function Foo(jsonObject) {
    var self = this;

    self.AdHocRecipients = ko.observableArray(jsonObject.AdHocRecipients);
    self.CriteriaStub = ko.computed(function () {

        return "No criteria";
    });

    self.AdHocRecipientsToStringStub = ko.computed(function () {
        var stub = "";
        for(var i in self.AdHocRecipients()) {
            stub += (self.AdHocRecipients()[i].User) + ', ';
        }
        return self.AdHocRecipients().length > 0 ? stub.slice(0, -2) : "No recipiants";
    });
    var t = 2;
}

另外,请小心你的html标签。虽然我认为自动关闭标签通常是有效的,但它们并不总是有效,所以请使用而不是。