question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Define Angular Custom element without ngModules

See original GitHub issue

Which @angular/* package(s) are relevant/related to the feature request?

elements

Description

Hello team, So without standalone on to define a custom element as you know we use below syntax:

export class AppModule implements DoBootstrap {
  constructor(private injector: Injector) {}

  public ngDoBootstrap(): void {
    const ce = createCustomElement(AppComponent, { injector: this.injector });
    customElements.define('likes-app-element', ce);
  }
}

But how should we do this without NgModules? I tried to add above snippet to AppComponent but seems it does not create custom element and we receive error: the selector "likes-app-root" did not match any elements. I think this is because we already bootstrap AppComponent in main.ts file. With ngModules we could prevent bootstrapping by specifying bootstrap: []. To prevent bootstrapping we could just create custom element in bootstrap.ts but we have to provide Injector there. Thanks.

Proposed solution

I think we should be able to define elements in AppComponent.

Alternatives considered

Alternative was trying to define element in AppComponent but not working as expected.

Version

Angular 14.0.0-rc.0 Plaground branch: https://github.com/vugar005/youtube-webapp-turborepo/tree/feature/preview-angular-v14.0.0-rc.0/apps/likes-app

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:15
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
pkozlowski-opensourcecommented, Jun 23, 2022

PR https://github.com/angular/angular/pull/46475 has a sketch of our initial thinking on the API, if anyone is interested. The gist of it is:

import {createRootEnvironment} from '@angular/platform-browser';
import {createCustomElement} from '@angular/elements';

const envInjector = await createRootEnvironment({providers: ...});
const MyCustomElementCtor = createCustomElement(MyCustomElement, {injector: env});

The concepts / details might change but the above captures our current thinking.

3reactions
pkozlowski-opensourcecommented, May 12, 2022

But how should we do this without NgModules?

We didn’t explore “Angular elements” without NgModules yet, this is on our roadmap, see: https://github.com/angular/angular/discussions/45554#discussioncomment-2716216

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular elements overview
Creating a custom element is simple and straightforward, and automatically connects your component-defined view with change detection and data binding, mapping ...
Read more >
Angular Elements: Web Components with Standalone ...
Angular's Future Without NgModules – Part 1: Lightweight ... The result is a custom element that can be registered with the browser using ......
Read more >
Developer Preview of Standalone Support in Angular Elements
One of the merged PRs includes creating custom elements without NgModule. In this post, we will take a look at how to create...
Read more >
Creating a Custom Element in Angular | by lavanya k - Medium
5. Update NgModule · The first parameter is of type string and contains the name of the element. Passing the string 'app-button' means...
Read more >
Create and use Custom Elements with Angular by Tamas Piros
Custom elements : a JavaScript API that allows us to define custom ... these custom elements as Angular components, but without any Angular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found