BrightonDev
BrightonDev

Reputation: 435

Databinding to a single item in a collection

If I have an observable array in my view model I can access individual items in that array using the item's position within my script:

alert(this.travellers()[0].Age);

However, I cant seem to databind to the item/property using similar syntax in my view:

<label data-bind="text: travellers()[0].Age" />

Again, I'm sure I'm missing something fundamental.

Upvotes: 0

Views: 282

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

Your syntax is correct, it just depends on where you are doing the binding to tell whether travellers is available at that level.

If you are inside of a template, then you would want to consider passing this value in via templateOptions or if your viewModel has global scope you could reference it like text: yourViewModel.travellers()[0].Age.

Sample with each option: http://jsfiddle.net/rniemeyer/brAtZ/

Upvotes: 1

Related Questions