Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 敲除、Web API和信号器-未捕获类型错误_Javascript_Jquery_Knockout.js_Ko.observablearray - Fatal编程技术网

Javascript 敲除、Web API和信号器-未捕获类型错误

Javascript 敲除、Web API和信号器-未捕获类型错误,javascript,jquery,knockout.js,ko.observablearray,Javascript,Jquery,Knockout.js,Ko.observablearray,我试图为ObservalArray的元素设置一个值,但得到一个无法解决的错误: Uncaught TypeError: Property 'Locked' of object #<Object> is not a function HTML-敲除绑定 <table data-bind="visible: Brands().length > 0" class="table table-striped table-bordered table-hover" id="bra

我试图为ObservalArray的元素设置一个值,但得到一个无法解决的错误:

Uncaught TypeError: Property 'Locked' of object #<Object> is not a function 
HTML-敲除绑定

<table data-bind="visible: Brands().length > 0" class="table table-striped table-bordered table-hover" id="brands">       
    <tbody data-bind="foreach: Brands">
        <tr>
            <td class="align-center">
                <!-- ko if: Logo -->
                <img data-bind="attr: { src: Logo.URL() + '?width=50&height=50' }" class="img-polaroid" />
                <!-- /ko -->
            </td>
            <td data-bind="text: Name()"></td>
            <td data-bind="date: DateChanged()" class="align-center"></td>
            <td class="align-center">
                <!-- ko ifnot: Locked -->
                <a data-bind="click: $root.Edit" class="btn blue tip" data-original-title="wijzigen"><i class="icon-edit"></i></a>
                <a data-bind="click: $root.ShowDeleteModal" class="btn red tip" data-original-title="verwijderen"><i class="icon-trash"></i></a>
                <!-- /ko -->
                <!-- ko if: Locked -->
                <div class="btn black"><i class="icon-lock"></i></div>
                <!-- /ko -->
            </td>
        </tr>
    </tbody>
</table>

分配值时我是否做错了什么?我也尝试过这样做:brand.Locked=true;。这次我没有收到任何错误,但是knockout没有响应。

问题是,当您调用ko.utils.arrayFirstself.Brands时,您的Brands不再是可观察的,而是作为属性而不是可观察函数“锁定”的常规javascript对象,这是因为,当您从服务器检索数据并将其推送到Brands数组中时,您并没有使用ko.array来包装它

尝试:

在getBrandById函数中:

self.getBrandById = function (Id) {
            return ko.utils.arrayFirst(self.Brands(), function (item) {
                if (item.Id() == Id) {
                    return item;
                }
            });
编辑:

实际上,由于Brands数组包含Brand对象,因此ko.observatarray不足以将每个品牌的内部属性也转换为obversable。您需要使用映射插件,如下所示:

success: function (data) {
             // add brands
             self.Brands = ko.mapping.fromJS(data);

更多关于ko.utils.mapping插件的信息。

当我看到那些不必要的评论时,我的眼睛会流血:/一个可能的问题是self.getBrandById没有打开Id ObservalethX Haim770,回答得很好。现在我已经用你的建议修改了我的代码,现在我的html无法绑定。请看编辑我的问题。我稍微修改了我的答案。现在尝试使用self.Brands=ko.mapping.fromJSdata;Thx Haim770。这对我来说不起作用,但我已经看得更远了,当我这样做时得到了结果:ko.mapping.fromJSdata,{},self.Brands;但是我仍然在将它绑定到HTML时遇到问题。我得到了名称和日期,但没有得到徽标的URL。您是否尝试过ko.mapping.fromJSdata,self.Brands?此外,由于映射插件将您的所有属性更改为obverable,请尝试:
self.getBrandById = function (Id) {
            return ko.utils.arrayFirst(self.Brands(), function (item) {
                if (item.Id() == Id) {
                    return item;
                }
            });
success: function (data) {
             // add brands
             self.Brands = ko.mapping.fromJS(data);