[英]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.