Reputation: 226
I have a really simple question, but can't understand what I am missing. Here is the code:
$(document).ready(function() {
var viewModel = {
test: ko.observable(null)
}
var obj = {
name: "name123"
}
viewModel.test(obj);
ko.applyBindings(viewModel.test, document.getElementById("a"));
alert(viewModel.test.name());
});
link to jsfiddle: http://jsfiddle.net/bwkAB/ Why I can't get the name property value?
UPDATE: It should be viewModel.test().name
Upvotes: 1
Views: 8443
Reputation: 2685
Here is the corrected jsfiddle, You need to make the child objects property observable in order to bind to it. As well as adding the with binding to help with scope in the HTML
$(document).ready(function() {
var viewModel = {
test: ko.observable(null)
}
var obj = {
name: ko.observable("name123")
}
viewModel.test(obj);
ko.applyBindings(viewModel);
alert(viewModel.test().name());
});
Upvotes: 3