Multiple instances of the same Angular 6 component

11,667

Solution 1

You can define your component to take the required properties as inputs and reuse the same component several times with different inputs.

In the example below you define the HelloComponent with a name property, the initialise multiple instances of HelloComponent with different inputs.

Child

export class HelloComponent  {
  @Input() name: string;
}

Parent

<hello name="Ben"></hello>
<hello name="Jack"></hello>
<hello name="Roger"></hello>

Demo

Solution 2

app.component.html

<data-component [cpu]="cpu" [usage]="usage" [otherData]="otherData"></data-component>
<data-component [cpu]="cpu1" [usage]="usage1" [otherData]="otherData1"></data-component>
<data-component [cpu]="cpu2" [usage]="usage2" [otherData]="otherData2"></data-component>
<data-component [cpu]="cpu3" [usage]="usage3" [otherData]="otherData3"></data-component>
<data-component [cpu]="cpu4" [usage]="usage4" [otherData]="otherData4"></data-component>

app.component.ts

here you can have logic for all the cpu,usage,other data values taken from different endpoints and initialise.

data.component.html

//you card logic.

likewise you can control various instance of the component via app.component.ts via models.

Share:
11,667
Alan Mark Kristensen
Author by

Alan Mark Kristensen

Xamarin.forms in C#, XAML: A large platform for managing employees and tasks between departments at one of the worlds largest hotel chains. Included an app built with Xamarin.Forms and a management website. Angular 6 and 4, typescript, Html/CSS: Using Angular 4 with PrimeNG, i was the main driver in building the frontend for a large ERP platform. I've also built multiple MEAN stack applications for various purposes, and have a good knowledge in both Mongo DB, Angular, Node and Express. Photoshop/Illustrator/Adobe XD: General software design. Over the past 9+ years I have designed several software solutions. My main expertice is in App design. Among the biggest projects were the eShout platform, the Xamarin.Forms project explained above, and the entire ERP solution with over 10.000 layers. Java: The backend of eSHout was running Java. I have worked with Java in school, and for personal projects as well. General web development: I have built many websites over the course of the past 10 years.

Updated on June 04, 2022

Comments

  • Alan Mark Kristensen
    Alan Mark Kristensen almost 2 years

    I'm building a dashboard displaying data from a large JSON object containing information on various servers. In the dashboard i will have 12 squares containing the exact same information but from different sources (CPU usage, ram usage, errors list etc.).

    So i'm wondering if it is possible to dynamically scaffold one component that accepts arguments, for example 3 numbers and a string for lets say ram, cpu, power and IP address (the actual thing is closer to 20 different datapoints), and then replicate it on init using an Angular 6 component, but passing different data to each of the 12 different instances of the same component. I'd assume that using Bootstrap i'd be able to format sizing and placement with a grid.

    A simple example would be highly appreciated!

    • Oliver
      Oliver almost 6 years
    • Oliver
      Oliver almost 6 years
      Or something like `<div *ngFor><my-component [source]=item></my-component></div>
    • Matthias
      Matthias over 5 years
      I upvoted because I think it's a valid question. However I think if you walk through the tour of heroes you might have a clue about how to solve this :-)
  • Don Thomas Boyle
    Don Thomas Boyle about 5 years
    The problem with this one is that when you are using something like radio buttons in the html the "name" attribute is carried over into another component of the same type