When should I use $parent?

I am very new to aurelia, I was going through the exercises and I came across $parent. That has direct access to the view model or to be more specific observers the values of the view model. In most cases you can use $parent.Method or $parent.Variable so why should you even bother with ${Variable} instead of ${parent.Variable}.

when should you really use $parent?

Aurelia automatically checks parent view-models when its binding to properties and methods so the use of $parent is rare. In our codebase I can’t even find any usages of $parent.

Check out all the supported contextual properties.

As the docs say, $parent is only required when a property on the current scope masks a property on the outer scope.

1 Like

I’m new to Aurelia too so my answer might not be the best, but here I go:

From the documentation at http://aurelia.io/docs/binding/basics#element-content:

$parent - Explicitly accesses the outer scope from within a compose or repeat template. You may need this when a property on the current scope masks a property on the outer scope. Chainable- eg $parent.$parent.foo is supported

Basically, when you have something that changes your scope (such as a “with” : http://patrickwalters.net/aurelia-data-binding-strategies/) you may still need to access a value from the outer scope (parent). If a property with the same name is present on both the child and the parent, it will chose the property from the closest scope (which in this case would be the child’s).

Here’s a gist of it:

It’s true that for "repeat"s I see it doesn’t actually change the scope…

1 Like