How to use foreach with a special first element?

后端 未结 4 814
名媛妹妹
名媛妹妹 2021-01-03 23:48

If I have an observable array

foos = [{ name: \"a\" }, { name: \"b\" }, { name: \"c\" }]

on my viewmodel, I would like to render the follow

4条回答
  •  时光取名叫无心
    2021-01-04 00:34

    As there is not currently a way to tell the template binding where to render the template, I don't see a cleaner way to do it right now other than something like:

    So, we are passing the first item in your array as templateOptions and checking for if the item that we are dealing with in the template is indeed the first.

    Sample here: http://jsfiddle.net/rniemeyer/XuXcr/

    Also templateOptions was added after 1.12 KO, so you would need current code. More info about templateOptions here.

    Hope this helps.

提交回复
热议问题