knockout.js主要是为了解决UI层与数据层绑定而产生的,这件主要学习knockout 中的observable(监控性)以及computed(依赖监控):
例如现在我要给一个firstName,lastName绑定数据,那么我可以直接这样写:先写下html
<!DOCTYPE html> <html> <head> <title>observable、 computed</title> </head> <body> <label>firstName</label><input data-bind="value:firstName" type="text" /> <label>lastName</label><input data-bind="value:lastName" type="text"/> <label>fullName<label><input data-bind="value:fullName" type="text"> </body> </html>
<script type="text/javascript"> var viewModel = function(first,last){ var self = this; self.firstName = ko.observable(first); self.lastName = ko.observable(last); self.fullName = ko.computed(function(){ return this.firstName +"" + this.lastName; },this); }; </script>