Knockout unable to process binding

前端 未结 1 1303
情书的邮戳
情书的邮戳 2021-01-23 22:01

How do I bind a text when it is undefined? For example name is not available:

1条回答
  •  萌比男神i
    2021-01-23 22:14

    You can you the $data binding context property which always represents the current view model to access the name through it:

      

With this approach KO won't throw an exception if one of the items in the records does not have a name property.

Without the $data the identifier named name is undefined. However $data.name is always a valid expression it just returns undefined if the current view model does not have a property named name.

0 讨论(0)
提交回复
热议问题