Contextual properties
Context properties in Aurelia 1 are special variables that allow you to access data and functionality from parent or ancestor components in your view templates. They're incredibly useful for navigating the component hierarchy without explicitly passing data through multiple levels.
Common context properties
$this
Represents the current binding context (view-model).
$parent
Accesses the outer scope from within a compose or repeat template. It's chainable for accessing higher-level scopes.
$event
Provides access to the DOM Event in delegate or trigger bindings.
Properties for repeat.for
The following properties are available within a repeat.for
template:
$index
The index of the current item in the collection.
$first
True if the current item is the first in the array.
$last
True if the current item is the last in the array.
$even
True if the current item has an even-numbered index.
$odd
True if the current item has an odd-numbered index.
Example usage:
Best practices
Don't overuse
$parent
. It can make your code harder to maintain. Consider using proper data binding or a state management solution for complex scenarios.Remember,
$parent
in a repeater (repeat.for
) refers to the repeater's parent, not the list's previous item.You can chain
$parent
like$parent.$parent
Use this sparingly to go up multiple levels as it creates tight coupling.$event
is great for handling DOM events, but remember to keep complex logic in your view-model.
Last updated
Was this helpful?