seroth
seroth

Reputation: 647

How do I access variables in custom elements in Aurelia?

I have a custom element on my page and need to access a variable on the app.js from the custom element. Is the only way to bind the data I need to the custom element?

<custom-element data.bind="data"></custom-element>

The reason I ask is that I am running a loop to create multiple custom-elements and don't want to bind 5 variables to each custom element like that. Just asking if there is another way?

Upvotes: 0

Views: 488

Answers (1)

Jeremy Danyow
Jeremy Danyow

Reputation: 26406

A custom element's template cannot access the outer scope. This is by-design. It ensures custom elements are portable. This mirrors the way standard elements work such as <div>, <a>, etc. You can use those elements anywhere. The only way to pass them data is via their attributes.

If you want to tightly couple your custom element to the outer view model, you could do something like this:

<custom-element app.bind="$this"></custom-element>

but this is not recommended

Upvotes: 2

Related Questions