如何从Knockout.js更新对象的值? [英] How to update value of an object from knockoutjs?
本文介绍了如何从Knockout.js更新对象的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用淘汰表将数据绑定到表,我想在用户单击时更新对象.这是我的代码
I'm using knockoutjs to bind data to table, and I want to update object at user click. Here is my code
var Books = [{Book:"Harry Potter",Author:"J.K rowling"},{Book:"5 Point Someone",Author:"Chetan Bhagat"},{Book:"I too had a love story",Author:"Ravinder Singh"}];
var appViewModel = function() {
this.firstName = ko.observable("Amit");
this.Books = ko.observableArray([]);
this.Books(Books);
this.updateBook = function() {
this.Book("Harry Potter and Prisoner of Azkaban");
}
};
ko.applyBindings(appViewModel);
但是它得到错误:未捕获的TypeError:字符串不是函数".如何解决?
But it get error: "Uncaught TypeError: string is not a function". How can fix it ?
请参阅: http://jsfiddle.net/jVQY8/8/
推荐答案
您还需要将每个Book
对象的属性也设置为observable
.为此,请使用映射插件(现已内置)
You need the properties of each of your Book
object to be an observable
as well. Use the mapping plugin (that is now built-in) for that:
this.Books = ko.mapping.fromJS(Books);
此外,当两次this
分别引用不同的对象时,也会出现两次误导.常见的方法是:
Also, it's a bit misleading to see those this
twice when each refers to a different object. The common approach is:
this.updateBook = function(book) {
book.Book("Harry Potter and Prisoner of Azkaban");
};
请参见小提琴
这篇关于如何从Knockout.js更新对象的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文