2

I have a component called NewCustomerComponent and I want to load and display it through a modal popup in another page/component when a button is clicked. I have written the relevant bit of code [or so it seems]. But I am getting the following error --

    this._childInstance.dialogInit is not a function
at ModalDialogComponent.dialogInit (modal-dialog.component.js:65)
at ModalDialogService.openDialog (modal-dialog.service.js:26)
at OrderNewComponent.newCl (order-new.component.ts:85)

My code is pretty simple too, in the component where I am trying to open the modal popup. I'll just post the relevant portions --

    import { Component, Inject, ViewContainerRef, ComponentRef } from 
             '@angular/core';
    import { Http, Headers } from '@angular/http';
    import { Router } from '@angular/router';
    import { Observable, Subject } from 'rxjs';
    import 'rxjs/add/operator/map';
    import { CustomerSearchService } from '../../../shared/services/customer- 
       search.service';
    import { ICustomer, Customer, CustomerD } from 
       '../../../shared/models/customer';
    import { ModalDialogModule, ModalDialogService, IModalDialog } from 'ngx- 
      modal-dialog';
    import { NewCustomerComponent } from 
      '../../../components/popups/customer/customer-new.component';


    @Component({
        selector: 'order-new',
        templateUrl: './order-new.component.html' 
    })

    export class OrderNewComponent {

    public reference: ComponentRef<IModalDialog>;

    constructor(private cusService: CustomerSearchService, private http: 
        Http, private modalService: ModalDialogService, private viewRef: 
        ViewContainerRef) {

    }

   ngOnInit(): void {

   }


  ** this is where I am trying to load the newcustomercomponent and open it 
  in the popup. not working.     
  newCl() {
     this.newC = true;
     this.exiC = false;

     this.modalService.openDialog(this.viewRef, {
        title: 'Add New Customer',
        childComponent: NewCustomerComponent
     });
   }

 }

** edits. NewCustomerComponent code added for reference.

    import { Component, Input, Output, EventEmitter, OnInit, 
       ChangeDetectorRef, Directive, ElementRef, Renderer, AfterViewInit } 
       from '@angular/core';
    import { Router, ActivatedRoute } from '@angular/router';
    import { NgFor } from '@angular/common';
    import { Observable } from 'rxjs/Rx';
    import { BehaviorSubject } from 'rxjs/Rx';
    import { PlatformLocation } from '@angular/common';
    import { Http } from '@angular/http';
    import { ICustomer, Customer } from '../../../shared/models/customer';
    import { UserService } from '../../../shared/services/UserService';
    import { IModalDialog, IModalDialogOptions, IModalDialogButton } from 
         'ngx-modal-dialog';
    @Component({
       selector: 'new-customer',
       templateUrl: './customer-new.component.html'
    })

    export class NewCustomerComponent implements IModalDialog {
       model: Customer = new Customer();
       errors: any;
       submitResponse: any;
       actionButtons: IModalDialogButton[];

       constructor(private userService: UserService, private http: Http) {
                  this.actionButtons = [
                  { text: 'Close', onAction: () => true }
               ];
       }


      ngOnInit() {

      }

      dialogInit(reference: ComponentRef<IModalDialog>, options: 
                       Partial<IModalDialogOptions<any>>) 
      {
                // no processing needed
      }

     createCustomer() {
          this.userService.createCustomer(this.model)
        .take(1)
        .subscribe(
            (response: any) => {
                this.submitResponse = response;

                if (response.success) {
                    console.log('New customer added!');
                }
                else {
                    console.log('Unable to add customer!');
                }
            },
            (errors: any) => this.errors = errors
        );
    return false;
}

     cancelClicked() {

     }

   }

What did I do wrong here? Has it got something to do with the element reference I added in terms of the viewRef? Which portion is erroneous? What about that child component? Does it require to have some specific configuration/markup/component for this to work? I am very new to angular; I am not sure whatever the reason is.

Kindly help me rectify this scenario. Thanks in advance,

Prabir Choudhury
  • 143
  • 1
  • 18

1 Answers1

0

Can you please ensure that the NewCustomerComponent is implementing the IModalDialoginterface. Also, if this is not the case can you please share the code of NewCustomerComponent as well.

edits

Looks like you have not defined the dialogInit method in the NewCustomerComponent and it didn't pop up before as you have not implemented the interface IModalDialog. I would request you to define the dialogInit method in the component class as suggested on the link.

  • hi there @AshishKhandelwal, I have added the NewCustomerComponent code. Pl take a look. Please note -- I am showing this same newcustomercomponent component elsewhere also, in simple normal manner, just as a normal page. Hope, adding the IModal interface won't hamper that in any manner. Just confirming, Thanks, – Prabir Choudhury Jun 29 '18 at 05:09
  • @PrabirChoudhury I have edited the answer. Have a look. – Ashish Khandelwal Jun 29 '18 at 12:07
  • hi @AshishKhandelwal thanks for the link. reading it now - will try to do it as per the guidelines in the doc; if farther complications I will revert back. – Prabir Choudhury Jun 30 '18 at 08:28
  • hi @AshishKhandelwal there is only 1 problem happening now - upon clicking the button, the modal is showing but for a very fraction of second only and then escaping away. kindly check the updated section of the code for the newcustomercomponent that I have implemented. why could it be happening? I experimented with value of true false both for onAction()=> Not working. – Prabir Choudhury Jul 01 '18 at 15:34
  • nope I am still stuck with this. I have done several changes; pl take a look if you please. @AshishKhandelwal.The dialog is displaying but closing almost immediately. automatically.There's no console errors showing so I cannot see for any message/indication there either. Someone who knows the reason, pl help. – Prabir Choudhury Jul 03 '18 at 17:20