简体   繁体   中英

Angular Modal: How to reuse the same html in modal as well as in the page

In my page whenever user clicks on the image . Whatever be on the page should be now shown in the modal. Currently I have copied the same html two times in my angular code . Is there any way we can reuse the same html for page as well as the modal also in angular.

You could create a component with the template html you want, and use it in both the page and the modal

You could also use ng-template and ng-container

@Component({
  selector: 'app-root',
  template: `      
<ng-template #estimateTemplate let-lessonsCounter="estimate">
    <div> Approximately {{lessonsCounter}} lessons ...</div>
</ng-template>
<ng-container 
   *ngTemplateOutlet="estimateTemplate;context:ctx">
</ng-container>
`})
export class AppComponent {

    totalEstimate = 10;
    ctx = {estimate: this.totalEstimate};
  
}

*example taken from here

I'm not pretty sure what did you looking for, I estimate that you like to reuse your component by dynamic content, then this is what I like to share.

image-modal.component.ts

 @Component({
   selector: 'app-image-modal',
   template: `
     <b>I receive contect: </b> <ng-content></ng-content>
   `
 })
 export class ImageModalComponent {
 
   constructor() {
   }
 
 }

app.component.html

<app-image-modal>
  <span> Hello </span>
</app-image-modal>

<app-image-modal>
  <span> World </span>
</app-image-modal>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM