0

我正在尝试学习淘汰赛并且遇到了问题。

我正在尝试使用 ko if 子句,但我自己似乎无法解决

到目前为止,我的脚本看起来像

  <script>
    var SimpleListModel = function (items) {
        this.questionType = ko.observable("");
        this.items = ko.observableArray(items);
        this.itemToAdd = ko.observable("");
        this.addItem = function () {
            if (this.itemToAdd() != "") {
                var qt = $("#question-type").data("kendoDropDownList");
                this.questionType(qt.value());
                console.log(qt.value());
                this.items.push(this.itemToAdd()); // Adds the item. Writing to the "items" observableArray causes any associated UI to update.
                this.itemToAdd(""); // Clears the text box, because it's bound to the "itemToAdd" observable
            }
        }.bind(this);  // Ensure that "this" is always this view model
    };
    $(document).ready(function () {
        ko.applyBindings(new SimpleListModel([]));
    });
</script>

我的 html 看起来像

     <button type="submit" class="btn pull-right" data-bind="enable: itemToAdd().length > 0"><i class="icon-plus"></i>Add Question</button>
                        <div id="questions" data-bind="foreach: items">
                            <div class="question-item">
                                <label data-bind="text: $data" class="q-label"></label>
                                <textarea placeholder="Answer" class="q-answer"></textarea>
                                 <!-- ko if: questionType()==="ABC" -->
                                           Display ABC
                                <!-- /ko -->
                                <!-- ko if: questionType()==="DEF" -->
                                           Display DEF
                                <!-- /ko -->
                            </div>
                            <div class="clear"></div>
                        </div>

我需要做什么才能让 ko if: questionType 正常工作?

我已按照建议更新了 questionType 的设置,但是出现错误Uncaught Error: Unable to parse bindings. Message: ReferenceError: questionType is not defined; Bindings value: if:questionType()==="Comment"

4

1 回答 1

3

因为questionType它是一个 observable,你需要将它作为一个没有参数的函数来调用来检索它的值。

因此,您的if陈述需要如下所示:

<!-- ko if: $parent.questionType() === "ABC" -->
    Display ABC
<!-- /ko -->
于 2012-08-04T03:07:59.793 回答