user9438065
user9438065

Reputation:

¿Close a modal of an ANGULAR 5 form?

Well I want that when the button is clicked the modal closes

enter image description here

main.component.ts here you could do something too

crear(form){


this._servicio.creararchivos(this.formulario).subscribe(data =>{

this.conseguir();
form.reset();

this._routes.navigate(['/main']);


}, error =>{
console.log('error al crear el archivo');

}  
);

my html I think here is the problem would need a function for the button to do the click I have tried with Bostrap dissmiss but I do not send the data just closes me

 <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" 
 aria-labelledby="exampleModalLabel" aria-hidden="true">
 <div class="modal-dialog" role="document">
  <div class="modal-content">
  <div class="modal-header">
 <h5 class="modal-title" id="exampleModalLabel">Subir Archivos</h5>
 <button type="button" class="close" data-dismiss="modal" aria-
 label="Close">
  <span aria-hidden="true">&times;</span>
  </button>
 </div>
 <div class="modal-body">

<form (ngSubmit)="crear(a)" #a="ngForm" class="">

<div class="form-group">
<label for="user_id">User_id</label>
     <!--<select class="form-control" id="exampleFormControlSelect1" 
 *ngFor="let 
   datos of archivo">-->

   <input type="text" class="form-control" name="user_id" 
   [(ngModel)]="formulario.user_id">
 <!-- </select> -->

 </div>

 <div class="form-group">
 <label for="titulos">Titulo</label>
 <input type="text" class="form-control" name="titulo" 
 [(ngModel)]="formulario.titulo">
</div>

 <div class="form-group">
 <label for="descripcion">Descripcion</label>
 <input type="text" class="form-control" name="descripcion" 
 [(ngModel)]="formulario.descripcion">
 </div>


 <div class="form-group">
 <label for="imagen">Imagen</label>
 <input type="text" class="form-control" name="imagen" 
 [(ngModel)]="formulario.imagen">
 </div>

<button type="submit" class="btn btn-primary" >SUBIR</button> //this <---

 </form>

  </div>
  </div>
  </div>
  </div>

Upvotes: 2

Views: 4942

Answers (2)

Suhag Lapani
Suhag Lapani

Reputation: 695

  1. Add the reference to bootstrap modal. like #exampleModals

    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" #exampleModals>

  2. create ViewChild in component.

    @ViewChild('exampleModals') exampleModals: ElementRef ;

  3. on clear method crear(a) hide the modal.

    crear(a) { this.exampleModals.hide(); }

Upvotes: 0

Safiyya
Safiyya

Reputation: 1393

You can use @ViewChild to refer your button in your component :

  1. Add a reference to the btn #btnClose

    <button #btnClose type="button" class="close" data-dismiss="modal" aria-label="Close">
         <span aria-hidden="true">&times;</span>
     </button>
    
  2. Create a @ViewChild in your component code

    @ViewChild('btnClose') btnClose : ElementRef 
    
  3. In the submit function, click the btn programtically

    crear(){
    ...
        this.btnClose.nativeElement.click();
    ...
    }
    

Upvotes: 4

Related Questions