I'm studying knockout js and happened error whats is the problem??
<span data-bind="text: fullname"></span>
<script type="text/javascript">
function AppViewModel() {
this.firstName = ko.observable('Bob');
this.lastName = ko.observable('Smith');
this.fullname = ko.computed(function() {
return this.firstname() + " " + this.lastname();
},this);
}
// <!-- ko.applyBindings(viewModel); -->
var vm = new AppViewModel();
ko.applyBindings(vm);
</script>
when I call this,
Uncaught TypeError: this.firstname is not a function
happen... what is wrong?
At
return this.firstname() + " " + this.lastname();
You did not capitalize the "n" in firstname and lastname. So try something like this:
function AppViewModel() {
this.firstName = ko.observable('Bob');
this.lastName = ko.observable('Smith');
this.fullname = ko.computed(function() {
return this.firstName() + " " + this.lastName();
},this);
}
// <!-- ko.applyBindings(viewModel); -->
var vm = new AppViewModel();
ko.applyBindings(vm);