r/angular • • 2d ago

ng-origin: Alt+Click any element in your Angular dev app to copy which component declared it, with file and line

Side project I just released. You hold Alt (⌥ on Mac) in your dev app, click an element, and this goes to your clipboard:

Page: /dashboard · route dashboard → DashboardPage (src/app/dashboard/dashboard-page.ts:13)
Element: <button> "Archive"
Inside: UiButton <button ui-button> (ui-kit) · variant=primary
Declared in: Widget (src/app/widget/widget.ts:15)
Projected into: CardShell (src/app/card-shell/card-shell.ts:13)
Template: src/app/widget/widget.html:2
Find: widget.archive
Chain: Widget > DashboardPage
Context: lang=en · viewport=1440×900 (xl)

The idea is to paste that into a coding agent together with what you want, like "change this text" or "add a second button here". Agents usually find the spot on their own, but they search for it first, and that costs time and tokens. With the block they skip the search and go straight to the file.

Setup

npm i -D ng-origin, then provideNgOrigin() in your providers behind isDevMode(). Production builds drop the runtime.

Things you might ask

  • Privacy: no network requests, no storage. Form field values are never read. Secret-looking strings like JWTs or API keys get redacted, and you can add your own patterns. It's best-effort though, so look before you paste.
  • Versions: the peer range is Angular 20 to 22, but I've only tested 20.3, and only in Chromium. If you're on 21 or 22, I'd like to know if it works.
  • Why 22.0.0 as the first version: the major follows the newest Angular major it supports.
  • Template line: needs Angular 22.1.4+ with enableTemplateSourceLocations. Without it that line is left out.
  • i18n: ngx-translate keys come through ng-origin/ngx-translate. Other i18n libs can plug in their own lookup.

Page with a film (under a minute) · GitHub · MIT

If it names the wrong component somewhere in your app, I'd love an issue with the block it produced.

8 Upvotes

6 comments sorted by

2

u/Discordbothost 21h ago

Publish a small repro app with expected origin blocks so people testing Angular 21/22 can compare results. That would make wrong-component reports much easier to reproduce, especially around projected content. Your README notes those debug internals can change between majors.

1

u/Old-Parsley-5826 14h ago

Most of that exists already, it's just not obvious from the README. [`projects/demo`](https://github.com/alexander-spies/ng-origin/tree/main/projects/demo) has the cases you mention: content projected into a card shell, a Material menu and select in the CDK overlay, a named outlet, and a template that another component renders with `ngTemplateOutlet`. [`e2e/block.spec.ts`](https://github.com/alexander-spies/ng-origin/blob/main/e2e/block.spec.ts) picks each of them in Playwright and checks the block line by line. Expected blocks, basically, just written as assertions.

The gap is versions. CI only runs Angular 20.3. A job for 21 and 22 is next on my list, because that's where `window.ng` could change under me.

Until then, if you get a wrong component, open an issue with the block and the few template lines around the element. That's usually enough for me to rebuild it in the demo.

1

u/AwesomeFrisbee 1d ago

Nice. I've mostly been using ng-inspect to get some details about a component (and mainly its current state) but its not really supported anymore and didn't really get an update with signals/standalone so its already a miracle that it still works.

Would be neat to get a new extension that includes a similar feature. I find that the current official Angular extension lacks ease of use for where I'm already looking in my devtools and find the page structure in the elements tab to make more sense to me. Not to mention easier to navigate and inspect other css stuff...

Regarding the ng-origin. Its kind of a shame it needs a provider to work. Would be neat if it worked on any angular app

1

u/Old-Parsley-5826 14h ago

The provider thing is fair. Most of the block comes from Angular's dev-mode debug API, and every dev build has that. What the provider adds is the app's injector, which is where the route gets read from, and the translation keys and the `context` callback need it too. Plus it keeps the tool opt-in per project, and prod builds drop it.

An extension that does the rest on any dev-mode app should be doable. How much of the block survives without the route and the keys, I'd have to try.

0

u/abiramcodes 1d ago

We already have one called ng-point : https://abiramcodes.github.io/ng-point/

1

u/Old-Parsley-5826 1d ago edited 1d ago

Cool, very similar idea! I just tried it out, but it doesn't really work with library components and similar use cases. 🙈 ng-origin seems to cover all scenarios and also gaves you the main page component from the route, so I guess ng-point isn't really on point after all