我有以下html:
<div data-bind="foreach: Contacts"> <a data-bind="click: $parent.Foo($data),text: Name">link</a> </div> <button data-bind="click: AddContacts">click</button>
和js代码:
var viewmodel = ko.mapping.fromJS({"Selected":null,"Contacts":[]}); viewmodel.AddContacts = function(){ this.Contacts([{"Name":"C1"},{"Name":"C2"}]); } viewmodel.Foo = function (contact) { alert(contact.Name); } ko.applyBindings(viewmodel);