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 开发者_JAVA技巧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.
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/
精彩评论