angulartypescriptangular-materialangular5ngx-modal

How to retrieve data from ngx-modal on submit angular 5


I am using ngx-modal to create dialog, below is the code in html

<button (click)="myModal.open()">Click Me</button>

<modal #myModal cancelButtonLabel="Cancel All 
[(ngModel)]="rfin" submitButtonLabel="submit" 
(onSubmit)="callSubmit()">

<modal-content >
<mat-card>
<mat-card-content   >
<h2 class="example-h2">Select Emp</h2><br>

<section class="example-section" *ngFor="let ep of emp" >

<ng-template>
<mat-checkbox [(ngModel)]="ep.checked">{{ep.name}}</mat-checkbox>
</ng-template>

</section>

</mat-card>
</modal-content>
</modal>

Here is code in my ts file

callSubmit(val)
{
Console.log(val);
}

But I am getting undefined. Please suggest

Component code added, here I am trying to access the data in console.

    import { Component,  Input, OnInit, Inject } from '@angular/core';
    import { ConfigService } from '../services/config.service';
    import { FormControl } from '@angular/forms';

    @Component({
    selector: 'app-emp,
    templateUrl: './emp.html',
    styleUrls: ['./emp.css'],
    })
    export class EmpComponent {
    rfin:any;
    empdata:any=[];
    emp:any=[];

    constructor(private configs: ConfigService){}

    callSubmit() 
    {
    Console.log(this.rfin);
    }

//getEmp is a service call for getting values from from api
getEmp() {

this.configs.doEmp().subscribe(

data => {this.empdata = data['emp'];

this.emp = this.empdata.map(o => {
return {

name: o.name, isData: o.isData, checked: false

};

});

},

err => console.error(err),         
() => console.log('done loading emp'));

}

 ngOnInit() {
        this.getEmp();
    }
}

updated the on-submit also


Solution

  • I am not sure about why you are using [(ngModel)]="rfin" in model popup section. But any way you could try this alternate way to read/write model object.

    You don't need to pass the model object in to onSubmit method. you can directly use in the method itself.

    (onSubmit)="callSubmit()"
    

    in code

    callSubmit()
    {
    Console.log(this.rfin);
    }
    

    Note: make sure, you are declare this model rfin object in your component class