简体   繁体   English

未捕获的TypeError:对象函数()没有方法项

[英]Uncaught TypeError: Object function () has no method items

I am working with binding a json data in a ul tag. 我正在使用ul标签中的json数据绑定。 It shows that an error has occured .Here is the code: 它表明发生了错误。这是代码:

<div id="newdiv" data-bind="visible: selectedSection() === 'ulClass', stopBinding: true ">
    <ul id="ulClass" data-bind="template: { name: 'templatesSample', foreach: items}">
        <script id="templatesSample" type="text/html">
            <li><span data - bind = "text:name" > </span>
            </li>
        </script>
    </ul>
</div>

The view model 视图模型

function names(firstnames) {
    this.name = ko.observable(firstnames);
}

var mappedData;
var viewmodel;

$(document).ready(function () {
    ko.bindingHandlers.stopBinding = {
        init: function () {
            return {
                controlsDescendantBindings: true
            };
        }
    };
    ko.virtualElements.allowedBindings.stopBinding = true;

    viewmodel = function () {
        items: ko.observableArray([]);
    };


    var namesOfPeople = '[{"Firstnames":"john"},{"Firstnames":"peter"},{"Firstnames":"mary"}]';
    var dataFromServer = ko.utils.parseJson(namesOfPeople);
    mappedData = ko.utils.arrayMap(dataFromServer, function (item) {
        return new names(item.Firstnames);
    });

    viewmodel.items(mappedData);

    ko.applyBindings(viewmodel, document.getElementById("ulClass"));
});

It shows following error in the console: 它在控制台中显示以下错误:

Uncaught TypeError: Object function ()
    {
         items:ko.observableArray([]);

    } has no method 'items' 

How can I fix the problem?Please suggest a solution. 我该如何解决问题?请提出解决方案。

Thanks 谢谢

You need to decide between initializing your vm as an object, or with a constructor function. 您需要决定将vm初始化为对象,还是使用构造函数。

This is a combination of both, mostly sintactically invalid I think :): 这是两者的结合,我认为大多数是无理的无效:):

viewmodel = function()
{
    items:ko.observableArray([]);
};

So either do this: 所以要么这样做:

viewmodel = {
                items:ko.observableArray([]);
            };

Or this: 或这个:

var VM=function() {
                     this.items = ko.observableArray([]);
                };

var viewModel = new VM();

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

相关问题 未捕获的TypeError:对象函数没有方法 - Uncaught TypeError: Object function has no method 未捕获的TypeError:对象函数()没有方法“替换” - Uncaught TypeError: Object function () has no method 'replace' 未捕获的TypeError:对象函数没有方法 - Uncaught TypeError: Object function has no method 未捕获的TypeError:对象[object Object]没有方法&#39;tagsInput&#39;(匿名函数) - Uncaught TypeError: Object [object Object] has no method 'tagsInput' (anonymous function) 未捕获的TypeError:对象函数(a){返回新j(a)}没有方法&#39;has&#39; - Uncaught TypeError: Object function (a){return new j(a)} has no method 'has' 未捕获的TypeError:对象没有方法“打开” - Uncaught TypeError: Object has no method 'on' 未捕获的TypeError:对象[object Window]没有方法&#39;each&#39;函数 - Uncaught TypeError: Object [object Window] has no method 'each' function 未捕获的typeerror:对象# <object> 没有方法“方法” - Uncaught typeerror: Object #<object> has no method 'method' 未捕获的TypeError:对象(JS函数)没有方法“应用” - Uncaught TypeError: Object (JS Function) has no method 'apply' Require.js:未捕获的TypeError:对象函数…没有方法 - Require.js : Uncaught TypeError: Object function … has no method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM