0

I'm using aurelia compose inside my application like this:

<compose view-model="childViewModel" model="{myActivationParameters: ...}"></compose>

How can I access the parent view model inside the childViewModel html code?

In the end I want to be able to do this inside the childViewModel.html

<span> ${ ParentViewModel.myProperty } and ${ RootViewModel.myOtherProperty } </span>

I'm looking for knockoutJs $root and $parent equivalent in aurelia.


edit: I'm using aurelia composition instead of custom components. Not a duplicate.

Justin Lessard
  • 10,804
  • 5
  • 49
  • 61
  • Possible duplicate of [Aurelia Custom Elements: Access Parent method](https://stackoverflow.com/questions/41390793/aurelia-custom-elements-access-parent-method) – Jesse Nov 02 '17 at 08:38
  • Have you tried $parent? ` ${ $parent.myProperty } and ${ RootViewModel.myOtherProperty } ` – Jeff G Nov 02 '17 at 11:20
  • 1
    @JessedeBruijne Not a duplicate. He uses a compose element, and not a custom element. – Valentin B. Nov 02 '17 at 13:49

1 Answers1

2

In a compose element, you have access to the context of his parent. So, just do ${myProperty}. If you have properties with the same name in the 2 viewmodels, you can't access the parent property.

In a future release, you will need to add the attribute inherit-binding-context in your compose element (See https://github.com/aurelia/templating-resources/issues/222)

Valentin B.
  • 255
  • 2
  • 8