Angular 4 get form value from modal

I am having trouble getting a form from a modal form when submitting the form. The log says addMountForm is undefined. I have provided the code snippets of my html as well as the component. I would be grateful for your help.

<ng-template #content let-c="close" let-d="dismiss">
   <div class="modal-header">
      <h3 class="modal-title">Add Mount Point</h3>
   </div>
   <div class="modal-body">
      <form (ngSubmit)="onSubmit()" #addMountForm="ngForm" >
         <div class="form-group">
            <label class="col-sm-2 control-label text-nowrap"
               for="archiveOrigin">Archive Origin</label>
            <div class="col-sm-10">
               <input type="text" class="form-control" ngModel id="archiveOrigin" name="archiveOrigin" placeholder="Archive Origin"/>
            </div>
         </div>

               <button type="submit" class="btn btn-default">Add</button>

      </form>
   </div>
   <div class="modal-footer">
      <button type="button" class="btn btn-default" (click)="c('Close click')">
      Close
      </button>
   </div>
</ng-template>
<div class="page pt-2">

</div>


@Component({
  selector: 'mount-point',
  templateUrl: './mountpoint.component.html',
  styleUrls: ['./mountpoint.component.scss']
})
export class MountPointComponent implements OnInit {

@ViewChild('addMountForm') addMountForm : NgForm;

  constructor(
    private modalService: NgbModal
  ){}




  open(content) {
      this.modalService.open(content).result.then((result) => {
         console.log("closed");
      }, (reason) => {
         console.log("dismissed" );
      });
   }

  onSubmit(){
    console.log("adding form values ");
    console.log(this.addMountForm);
}

}
+6
source share
3 answers
  • Use [(ngModel)]="value"instead ngModel.

  • Change (ngSubmit)="onSubmit()"to(ngSubmit)="onSubmit(addMountForm)"

    and in the component

    onSubmit(form: NgForm){
      console.log(form.value);
    }
    
+4
source

Your code was right. Like others, you need to pass the form through the onSubmit method because it is inside the template and therefore you cannot access it using @ViewChild.

onSubmit(form){
console.log("adding form values ");
console.log(form.value.archiveOrigin);
}

Plunker

, [()] ngModel, .

.

+2

1) Import NgForm

import {NgForm} from '@angular/forms';

2) Change your form definition to

<form (ngSubmit)="onSubmit(addMountForm)" #addMountForm="ngForm" >

3) Change your component to

 import { Component, OnInit } from '@angular/core';
 import {NgForm} from '@angular/forms';
 @Component({
  selector: 'mount-point',
  templateUrl: './mountpoint.component.html',
  styleUrls: ['./mountpoint.component.scss']
})
export class MountPointComponent implements OnInit {

  constructor(
    private modalService: NgbModal
  ){}

 ngOnInit(){

  }

  open(content) {
      this.modalService.open(content).result.then((result) => {
         console.log("closed");
      }, (reason) => {
         console.log("dismissed" );
      });
   }

  onSubmit(addMountForm: NgForm){
    console.log("adding form values ");
    console.log(addMountForm.value);
}

}

This will work, and you will get an object on the console containing all the values ​​of the form.

0
source

Source: https://habr.com/ru/post/1017008/


All Articles