简体   繁体   English

函数无法在剔除中访问observableArray

[英]Function not able to access observableArray in knockout

I keep getting this error when I try to update an observableArray: 'Unable to get property 'removeAll' of undefined or null reference' 当我尝试更新observableArray时,我一直收到此错误:“无法获取未定义或空引用的属性'removeAll'”

Here is my code: 这是我的代码:

var agilityFirmViewModel = {
    test: ko.computed(function () {
        return Pairs[0].Pairs;
    }),

    currentLocation: ko.observableArray(Pairs[0].Pairs[0].Location);

//function to update values in currentLocation
 ChangeLocation: function (practiceGroup) {
        if (practiceGroup === undefined) {
            practiceGroup = '(All Practice Groups)';
        }
        for (var i = 0; i <= Pairs[0].Pairs.length; i++) {
            if (practiceGroup.Department === Pairs[0].Pairs[i].Department) {
                this.currentLocation.removeAll();
                this.currentLocation.push(practiceGroup.Location[i]);
                return root.currentLocation;
            }
        }
    },
}

$(document).ready(function () {
    ko.applyBindings(agilityFirmViewModel);
});

Assuming that it goes through the if statement inside the for loop everytime, whenever it gets to 假设它每次都在for循环中通过if语句,

this.currentLocation.removeAll();

I get the error above saying that currentLocation is undefined. 我收到上面的错误,说currentLocation是未定义的。 I know that currentLocation has data in it, so would could be causing this issue. 我知道currentLocation中有数据,因此可能会导致此问题。 It's probably something simple, but I can't seem to figure it out. 这可能很简单,但我似乎无法弄清楚。 Thank you. 谢谢。

Where i call ChangeLocation(): 我在哪里调用ChangeLocation():

       <li class="nav-sidebar-GreenMenu">
                    <a href="javascript:;" class="draggable-panel" draggable="true">
                        <i id="CategoryIcon" class="glyphicon glyphicon-user"></i>
                        <span class="sidebarTabName"><b>Practice Group</b></span><br />
                        <!--For some reason, changing the margin-left below will change the width of the entire sidebar-->
                        <span id="CurrentItem_TPG" class="currentItem" @*style="margin-left:64px;"*@>(All Practice Groups)</span>
                        <span id="DefaultItem_TPG" style="display:none;">(All Practice Groups)</span>
                        <i class="fa fa-chevron-left pull-right hidden-xs" data-bind=""></i>
                        <span class="arrow open" style="margin-top:-30px;"></span>
                    </a>
                    <ul class="sub-menu">            
                        <!--ko foreach: test()-->
                            <li>  
                                <a href="#" data-bind="text: $data.Department, value: $data.Department, click: $root.ChangeLocation.bind($data.Department)" class="active"></a>
                            </li>
                        <!-- /ko -->
                    </ul>
                </li>

The problem is the way you're using bind . 问题是您使用bind的方式。 The actual signature for bind is: bind的实际签名为:

fun.bind(thisArg[, arg1[, arg2[, ...]]]) fun.bind(thisArg [,arg1 [,arg2 [,...]]])

Meaning, the first argument determines what this means in the function and the rest sets the arguments to use when it's called. 含义,第一个参数决定了this是指在功能,其余设置参数时,它被称为使用。 Instead, try changing your click handler to this: 相反,尝试将您的点击处理程序更改为此:

$parent.ChangeLocation.bind($parent, $data);

This ensures that ChangeLocation is called with $parent === this and $data === practiceGroup 这样可以确保使用$parent === this$data === practiceGroup调用ChangeLocation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM