Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass in generic HTML to an Angular 2 component?

I want to make a generic modal component that can hold anything, from just text to images/buttons etc. If I do something like this:

<div class="Modal">
    <div class="header"></div>
    <div class="body">{{content}}</div>
    <div class="footer"></div>
</div>

I'm not able to actually pass HTML into content, just text. How can I create a component such that the parent component can pass in whatever HTML it wants? What if I wanted to add n number of buttons to the footer, each with it's own callback? Is there a better way I should be doing this?

like image 566
user3715648 Avatar asked Aug 22 '16 17:08

user3715648


People also ask

What is generic component in angular?

In TypeScript, generics can be used to better describe the types in classes that are reusable with multiple derived types. Angular does not support instantiation of generic components, but this limitation can be worked around by creating a derived component for each derived type we want to use it with.


1 Answers

What you are looking for is ng-content

 <div class="Modal">
   <div class="header"></div>
   <div class="body">
     <ng-content></ng-content>
   </div>
   <div class="footer"></div>
 </div>

and you may pass any HTML content directly into your component. Lets say your component name is my-modal, you may use it like below,

  <my-modal>
     <<HTML content :  this will be replaced in the ng-content area >>
  </my-modal>

Hope this helps!!

like image 159
Madhu Ranjan Avatar answered Oct 18 '22 02:10

Madhu Ranjan