• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Access The Aurelia Controller/App Instance From Your Browser

Aurelia 1 · July 4, 2016

One of the lesser known abilities of Aurelia is that every Aurelia element has an au property on it. This means if you query the DOM for an Aurelia element, it will have an au property which contains app and controller.

While this is really handy to know, in most cases if you are a Chrome user, then the Aurelia Context plugin for Chrome is probably a safer bet as it integrates into the Chrome developer tools.

A convenient test of accessing the au property is to open up Chrome, go into your Aurelia app and then paste the following into the console:

document.querySelector('[au-target-id]').au.controller;

This will allow you to access the underlying view-model via the viewModel property on the controller as well as numerous other properties. I am not going to go through each property, but this is handy if you’re wanting to access an elements view-model (perhaps from a jQuery plugin, etc) and act accordingly.

The more you know.

Dwayne

Leave a Reply Cancel reply

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Eike
Eike
6 years ago

it’s easier to just select a DOM element in your dom inspector and then execute
$0.au.controller

$0 always references to the currently selected dom element in developer toolkits (both chrome and FF)

0
kubrt
kubrt
6 years ago

Does this still apply?

None of the browsers I tried have the .au property on the element returned by the querySelector.

Neither does the $0 mentioned above…

0
loaded02
loaded02
6 years ago

It works for me in Chrome. Thx ๐Ÿ™‚

0
Kiran Deore
Kiran Deore
5 years ago

It gives me undefined for au property. Is this still supported?

0
Daniel
Daniel
5 years ago

Yes, it is still working. You might just not have the property on your au object.
Try just using “au”….

document.querySelector(‘[au-target-id]’).au
$0.au (if you follow Eike’s suggestion)

0
Martin
Martin
5 years ago

For me, on Aurelia Framework v1.1.2 I couldn’t find the .au property, but was worked was either;

document.querySelector(‘body’).aurelia.container.viewModel or
document.querySelector(‘body’).aurelia.root.viewModel

0

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • Handling Errors with the Fetch API
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • ChatGPT Stopping Part Way Through a Response? Here Is How You Fix It
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Mock uuid In Jest

Recent Comments

  • CJ on Microsoft Modern Wireless Headset Review
  • Dwayne on Microsoft Modern Wireless Headset Review
  • CJ on Microsoft Modern Wireless Headset Review
  • john on Microsoft Modern Wireless Headset Review
  • Dwayne on Why You Should Be Using globalThis Instead of Window In Your Javascript Code

Copyright © 2023 ยท Dwayne Charrington ยท Log in

wpDiscuz