1

I have a little problem with my system. I'm trying to add a component that i've I created, to the template of another component, it goes like this:

  1. I have a menu component which has many buttons, when each button is pressed it sends a parameter which indicate what type of information to show and redirect to some other page (to different url).

  2. There, in the other page I take the parameter (which is a string), comparing in to a array and for each node in the array, which its key is the same as the parameter, I need to create an instance of my component with the relevance information and display it on the page. There maybe sometimes only one match and sometimes 4-6 matches.

My problem is that I can't understand how to put my component tag into this page template.

for example: my component called "viewInfo" and it get a paramter called "data", I want to add it to the template like this:

...
<viewInfo [data]='newData'></viewInfo>
...
...

So when I add it manually, it works, but in run-time I can never know how many node in the array will be the match the parameter.

Anyone has some kind of idea that could help me?

Thanks.

Dorni
  • 699
  • 1
  • 5
  • 13
  • See https://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 for an example. – Günter Zöchbauer Oct 22 '17 at 17:39
  • Use ngFor with new array that only contains the matched node. If none are matched it won't render anything – Ahmad Oct 22 '17 at 17:42
  • I had the same problem the error in thinking is that I was trying to implement it like React as you are doing. Angular does offer Dynamic Component Injection but it woulf get way complicated and had few bugs at the time I was trying it – Ahmad Oct 22 '17 at 17:44
  • Check this source if you decide to go with Dynamic Component Loading https://angular.io/guide/dynamic-component-loader – Ahmad Oct 22 '17 at 17:47
  • 1
    read this article that explains dynamic components in depth [Here is what you need to know about dynamic components in Angular](https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e) – Max Koretskyi Oct 22 '17 at 18:00
  • @AngularInDepth.com that is a good reason not to use angular. – Aluan Haddad Oct 23 '17 at 02:34

0 Answers0