0

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/

2
  • 1
    You don't need the dataTest(). qualifier within the foreach -- it should just say: span data-bind="text: SalePrice"' Commented Oct 5, 2015 at 14:17
  • 1
    If you do still need to reference the current item directly, you can use $data, as in data-bind="text: $data.SalePrice". More common if you need to pass it off to another function etc. Commented Oct 5, 2015 at 14:18

1 Answer 1

2

You are inside binding foreach. Use

<span data-bind="text: SalePrice"></span>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.