0

I have a resusable custom Material UI Dialog and I wanted to show a different component in it. For example onne time call it and show a Login component and another time show a registeration component, that is just example.

The problem is when I assign my component to body (body: InvitationComponent) the result is a correct dialog box but the body (or content) is a code of my component. When you send a text, that is ok but when I want to send a component to show in middle of the Dialog that is not possible. The question is how some one can send a component as an object or template to show in a dialog? Invitation component is made of shome html code and iput controls and a button and I wanted to show it in middle of my dialog. (for example like a page that we can can show in iframe) Thanks in advance for your help.

    <div class="title">
        <h2 mat-dialog-title>{{ data.title }}</h2>
        <span mat-dialog-close class="material-icons closeButton">close</span>
    </div>
    <hr>
    <div>
        <mat-dialog-content class="mat-typography">
            {{ data.body }}
        </mat-dialog-content>
        <hr>
        <div class="actionSection">
            <mat-dialog-actions>
                <button mat-button class="secondary" mat-dialog-close>{{ data.cancelButton }}</button>
            </mat-dialog-actions>
        </div>
    </div>
</div>

my shown dialog

my calling code is:
const dialogRef = this.dialogService.open(CustomDialogComponent,
      {
        hasBackdrop: true,
        disableClose:true,
        data:{
          title: 'Invite User',
          body: InvitationComponent,
          cancelButton: 'Close' }

that is my CustomDialogComponent:

export class CustomDialogComponent implements OnInit {

  constructor(@Inject(MAT_DIALOG_DATA) public data: any,
    private dialogRef: MatDialogRef<CustomDialogComponent>) {
    console.log(data.body);
  }

  ngOnInit() {
  }
baldr
  • 2,891
  • 11
  • 43
  • 61

1 Answers1

0

You can just have two different templates in the common dialog component html and rendering any one of it based upon the call. i have made a stackblitz example for better understanding: https://stackblitz.com/edit/angular-mat-dialog-kfdktu

rehan meer
  • 28
  • 7
  • Thanks Rehan, I said those two just as an example, that is a good solution, but I wanted to say that I have some other bare components and I wanted to show them into a dialog with customised title and button, just I wanted to inject my Component into a dialog showing content section. I do nöt know that I can say what I mean or not. Again thanks for your answer :) Just consider I have two or more seperated components and I wanted to inject these component into my customised dialog with favorite title and button description. – HamidFaridi Oct 20 '20 at 13:52
  • i have updated my stackblitz example considering your requirements. Kindly go through it again. Consider accepting the answer if it's right :) – rehan meer Oct 21 '20 at 06:00
  • Thanks alot Rehan, that is the possible solution.I spend so much time but I am new to Angular and have not a good knowledge in some parts. – HamidFaridi Oct 21 '20 at 06:27
  • Just another question, I tried your code and mine is a few different but I have an error: 'this.vcRef' is undefined, by the way my Angular cli version is 10 – HamidFaridi Oct 21 '20 at 06:58
  • when I log the 'this' in console vcRef object exists but when I log this.vcRef that is 'undefined'!!! – HamidFaridi Oct 21 '20 at 07:23
  • I am glad it helped you Hamid. Please share more details, It's difficult to find out the issue with the info provided. – rehan meer Oct 22 '20 at 18:49
  • Thanks for replying, I have your lines of code in mine: const comp = this.resolver.resolveComponentFactory(this.data.component); console.log(this.vcRef); //undefined console.log(this); //​​vcRef: Object { _lContainer: (10) […], _hostTNode: {…}, _hostView: (87) […] } this.componentRef = this.vcRef.createComponent(comp); //ERROR TypeError: this.vcRef is undefined When runs, I get an error, I wanted to say vcRef in 'this' has value but when I use it I get an error that says vcRef is undefined! but vcRef is => Object { _lContainer: (10) […], _hostTNode: {…}, _hostView: (87) […] } – HamidFaridi Oct 23 '20 at 13:44
  • In Angular 10 I implemented ngOnInit codes in ngAfterViewInit to loose my problem – HamidFaridi Nov 04 '20 at 09:50