javascript - How can I pass in generic HTML to an Angular 2 component? - Stack Overflow

I want to make a generic modal ponent that can hold anything, from just text to imagesbuttons etc. If

I want to make a generic modal ponent 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 ponent such that the parent ponent 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?

I want to make a generic modal ponent 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 ponent such that the parent ponent 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?

Share Improve this question edited Aug 22, 2016 at 18:45 CaptainBli 4,2114 gold badges42 silver badges59 bronze badges asked Aug 22, 2016 at 17:55 user3715648user3715648 1,5883 gold badges16 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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 ponent. Lets say your ponent 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!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744794030a4594094.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信