How to register multiple custom elements in angular element on a single page?

VANDANA GUPTA
2 min readMar 20, 2021

--

Photo by Ilya Pavlov on Unsplash

Let me give you a brief about the angular element before heading ahead.

By using Angular elements, you can package angular components as custom elements (one of the main specifications of web components) which gives the flexibility to use angular components in non-angular environments. In much simpler words, you can create custom DOM elements that have their own functionality such that the rest of the page doesn’t know anything about it.

@angular/elements package provides the functionality of angular elements. It exposes the createCustomElement() function which can be used to create a custom element from the component class.

Registering multiple components

Consider that you want multiple angular elements within a single application to work flawlessly on a single page. So for that, you have to register them.

Let’s see the possible ways to achieve this-:

  • First way

If we closely look at the above code, we need to register one by one which might be time taking if we have many components.So, let’s see a second way with the use of data structure array.

  • Second way

In the above code, we have created an elements variable which is an array of arrays, and hence while registering we can loop over it which decreases the time in writing repetitive code and decreases the number of lines of code.

You can even alter this solution by using arrays of objects.

Thank you ✨✨

Happy coding !!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

VANDANA GUPTA
VANDANA GUPTA

Written by VANDANA GUPTA

An inquisitive person. A learner.

No responses yet

Write a response