【问题标题】:¿Close a modal of an ANGULAR 5 form?¿ 关闭 ANGULAR 5 形式的模态?
【发布时间】:2018-03-04 21:31:46
【问题描述】:

我希望当点击按钮时模式关闭

main.component.ts 在这里你也可以做点什么

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');

}  
);

我的 html 我认为这里的问题是按钮需要一个函数来执行我尝试使用 Bostrap dissmiss 的点击,但我不发送数据只是关闭了我

 <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>

【问题讨论】:

    标签: angular twitter-bootstrap bootstrap-modal


    【解决方案1】:

    您可以使用@ViewChild 在您的组件中引用您的按钮:

    1. 添加对 btn 的引用 #btnClose

      <button #btnClose type="button" class="close" data-dismiss="modal" aria-label="Close">
           <span aria-hidden="true">&times;</span>
       </button>
      
    2. 在你的组件代码中创建一个@ViewChild

      @ViewChild('btnClose') btnClose : ElementRef 
      
    3. 在提交函数中,click btn 以编程方式

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

    【讨论】:

      【解决方案2】:
      1. 添加对引导模式的引用。比如#exampleModals

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

      2. 在组件中创建 ViewChild。

        @ViewChild('exampleModals') exampleModals: ElementRef ;

      3. 关于清除方法crear(a)隐藏模态。

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

      【讨论】:

        猜你喜欢
        • 2018-10-10
        • 1970-01-01
        • 2022-12-04
        • 2018-07-15
        • 1970-01-01
        • 2022-08-05
        • 2019-09-11
        • 2023-03-22
        • 1970-01-01
        相关资源
        最近更新 更多