Knockout 无法处理 'foreach' 的绑定

Knockout unable to process biding for 'foreach'

我是淘汰赛新手。对于我的问题,我试图让每个项目都有一个按钮和文本区域。文本区域将在页面加载时隐藏。如果我单击该按钮,它将显示文本区域,或将其隐藏(切换)。我实现了以下代码,但出现此错误:

Uncaught ReferenceError: Unable to process binding "foreach: function (){return projects }" Message: Unable to process binding "visible: function (){return show }" Message: show is not defined

查看模型:

function ProjectViewModel(proj) {
            //console.log(proj);
            var self = this;
            self.projects = ko.observableArray(proj);
            self.show = ko.observable(false);
            self.toggleTextArea = function () {
                self.show(!self.show());
            };
        };

HTML:

                <!-- ko foreach: projects -->
            <div id="eachOppyProject" style="border-bottom: 1px solid #eee;">
            <table>
                <tbody>
                    <tr>
                        <td><a data-bind="attr: { href: '/tools/oppy/' + guid }" style="font-size: 25px;"><span class="link" data-bind="    value: guid, text: name"></span></a></td>
                    </tr>
                    <tr data-bind="text: projectDescription"></tr>
<%--                    <tr data-bind="text: guid"></tr>--%>
                </tbody>
            </table>
                 <span class="forminputtitle">Have you done project this before?</span>  <input type="button" value="Yes" data-bind="click: $parent.toggleTextArea" class="btnOppy"/>
                <textarea placeholder="Tell us a little of what you've done." data-bind="visible: show, attr: {'id': guid }" class="form-control newSessionAnalyst" style="height:75px; " /><br />
                <span> <input type="checkbox" name="oppyDoProjectAgain" style="padding-top:10px; padding-right:20px;">I'm thinking about doing this again. </span>
            <br />
                </div><br />
                <!-- /ko -->

您收到的错误的重要部分是:

Message: show is not defined

这发生在你打电话的地方:

data-bind="visible: show ... "

发生这种情况是因为 Knockout 试图在您的 projects object 中寻找名为 show 的变量,但没有找到 showprojects 都是 ProjectViewModel 的 children。

要引用 show,您可以使用 $parent.show 代替:

data-bind="visible: $parent.show ... "