Binding contexts
are a hierarchical structure of special objects that help you make logical scopes for the control flow bindings. Each such binding (such as foreach
, if
, with
, and so on) generates its own binding context in the hierarchy. You can think of contexts as a scoped wrapper of a ViewModel. For example, the root binding context refers to your main ViewModel. You can work with contexts with the help of special properties that are listed as follows. The best way to understand the binding context concept is to look at some examples. Let's do it.
$parent
: This refers to the ViewModel of the parent binding context. For example, say you have theforeach
binding to the book collection, but want to use the name of the library insideforeach
. The parent context helps you as follows:<div data-bind="foreach: Books"> The book "<span data-bind="text: Name"></span>" from the "<span data-bind="text: $parent.LibraryName"></span>" library. </div...