knockout.js - Unable to parse bindings. knockout error -
this day 2 learning knockout.
trying attach "deleteitem" button click. gives following error.
error
uncaught error: unable parse bindings.
message: referenceerror: deleteitem not defined; bindings value: click: deleteitem
javascript:
$(function () { var defaultdata = [{ id: 1, item: "todo 1" }, { id: 2, item: "todo 2" }, { id: 3, item: "todo 3" }]; var viewmodel = { listitem: ko.observablearray(defaultdata), additem: function () { // add new item var id = this.listitem().length + 1; this.listitem.push({ id: id, item: "todo " + id }); }, deleteitem: function () { alert(this); } } ko.applybindings(viewmodel, main); });
html:
<div id="main"> <button data-bind="click: additem">+ add item</button> <div data-bind="foreach: listitem"> <input type="text" data-bind="value: item" /> <input type="button" data-bind="click: deleteitem" /> <br /> </div> </div>
the function deleteitem
on view model. when you're binding inside foreach
, context of binding operation individual item
listitem
array. need bind $root.deleteitem
reference root view model.
Comments
Post a Comment