1

我不明白为什么这里的第二项没有渲染,即只有一个 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;
})();
4

1 回答 1

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 标签。虽然我认为自闭合标签通常是有效的,但它们并不总是有效,所以使用而不是 .

于 2013-03-06T15:43:43.813 回答