DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Enterprise Authentication With Angular, Spring Boot, SSO, and MFA
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems

Trending

  • Understanding the Database Connection Pool (DBCP) Properties
  • Member Spotlight: Raghava Dittakavi
  • Understanding Vector Databases: Concepts, Similarity Search, and Trade-offs for Enterprise Applications
  • Pitching Kotlin: An Engineering Leader's Guide to Legacy Modernization

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. How to Create High Order Components With Angular

How to Create High Order Components With Angular

Learn how to create High Order Components with Angular and how to send Components via @input() into another component using examples from a dynamic wrapper.

By 
Eugene Sincovschi
·
Nov. 09, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

I was working with React Projects for a long time. I have evolved a mindset that allows you to abuse HOC to make code more reusable and somewhat clean. But when I got into Angular, it felt weird at first that there is no such concept.

The Spark

At some point, I got in touch with Modals and there it was the gong that awoke the ninja in me. First, I dove again into angular.io docs and indeed found a way to pass components through @Input() and render them. But that was unsatisfying because of the directive. That could be because decorators are something new after React. Or because it splits such a simple concept as HOC into too many files.

The Sandbox

Here I have created a small code sandbox after my researches. You can see how a component can be rendered from a variable. I got deeper into Angular docs. Found a matching pattern between template variables and @directive. The directive was necessary only to extract reference to the element. As a result, HOC got more simple and it is more close to the concept I got used to:

Ionic Modal Angular

TypeScript
async openModal() {
  const modal = await this.modalController.create({
    component: ModalWrapperComponent,
    componentProps: {
      bodyComponent: ModalComponent,
      bodyComponentInputs: { key: 'value' }
    },
  });
  modal.present();
}


Material Dialog Angular

TypeScript
openDialog(): void {
  const dialogRef = this.dialog
    .open(ModalWrapperComponent, {
      data: {
        bodyComponent: ModalComponent,
        bodyComponentInputs: { key: 'value' }
      },
    });
}


The Takeaway

I hope this article helps anyone that wants to understand more deeply how Angular works and how to be a ninja with HOC in Angular. I went geek-serk after a few weeks of Angular without this pattern. And this article emerged because there were no good answers out there in the wild about passing components to components. 

For those who want a HOC with a less dynamic inner component. Here is a version with *ngTemplateOutlet. The difference is that this is more simple and cannot pass any inputs into the wrapped component.

AngularJS

Published at DZone with permission of Eugene Sincovschi. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building Enterprise Authentication With Angular, Spring Boot, SSO, and MFA
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook