function AppViewModel() {
var self = this;
this.firstName = ko.observable("why");
this.lastName = ko.observable("520crazy");
//this.fullName = ko.computed(function() {
// return this.firstName() + " " + this.lastName();
//}, this);
this.fullName = ko.computed({
read: function () {
return this.firstName() + " " + this.lastName();
},
write: function (value) {
var array = (value || "").split(" ");;
self.firstName(array[0] || "");
self.lastName(array[1] || "");
},
owner: this
});
}
ko.applyBindings(new AppViewModel());
<fieldset ng-controller="simple">
<legend>例子</legend>
<p>First name: <input data-bind="value:firstName" /></p>
<p>Last name: <input data-bind="value:lastName" /></p>
<p>Hello, <input data-bind="value:fullName"></p>
<div data-bind="text: fullName"></div>
</fieldset>