I was taking a look at another question (Knockout.js how to access the inner object property on data-bind) and saw how to bind a property within an object. I was wondering how to bind a property from multiple objects within an array and assumed it would be a quick tweak on the answer:
var data = [
{
"Id" : 1001,
"SalePrice" : 12345,
"ListPrice" : 333,
"ShortDesc" : "Tayler 12345E",
"Description" : " Long Description"
},
{
"Id" : 1002,
"SalePrice" : 23456,
"ListPrice" : 444,
"ShortDesc" : "Tayler 23456F",
"Description" : " Long Description"
}
];
var viewModel={
dataTest: ko.observable(data)
};
ko.applyBindings(viewModel);
<div data-bind="foreach: dataTest">
<span data-bind="text: dataTest().SalePrice"></span>
</div>
I'm getting an error saying that dataTest is not defined, but I can't work out why this is happening. Could someone take a look and let me know where I'm going wrong?
Amended Fiddle here: http://jsfiddle.net/nimaek/sZYcn/132/
dataTest().qualifier within theforeach-- it should just say:span data-bind="text: SalePrice"'$data, as indata-bind="text: $data.SalePrice". More common if you need to pass it off to another function etc.