Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a dynamic modal using angular 2

I want to make a custom modal. The problem is I dont know how to dynamically load the template url into the modal. I can do everything but dynamically load the url or another similar approach.

Thanks, MW

like image 237
matt2405 Avatar asked May 12 '26 04:05

matt2405


1 Answers

It's simple! Just create a component that represents your modal and use ngTransclusion to insert html inside the component. Example:

// my-modal.component.ts
import {Component} from 'angular2/core';

@Component({
  selector: 'my-modal',
  template: `
    <div>
      <ng-content></ng-content>
    </div>
  `
})
export class MyModal{}

using the modal:

// app.component.ts
import {Component} from 'angular2/core';
import {MyModal} from './my-modal.component';

@Component({
  selector: 'my-app',
  template: `
    <div class="app">
      <my-modal>
        This is my transcluded content inside my modal!
      </my-modal>
    </div>
  `,
  directives: [
    MyModal
  ]
})
export class AppComponent {}

NgTransclusion is a builtin tool, so you don't need to import nothing!

like image 137
Reginaldo Camargo Ribeiro Avatar answered May 15 '26 06:05

Reginaldo Camargo Ribeiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!