如何使用 angular 关闭引导模式

How to close a bootstrap modal using angular

提问人:Zidane 提问时间:11/3/2023 更新时间:11/3/2023 访问量:50

问:

我有一个引导 5 模式,我正在尝试从打字稿中关闭它。当我调用函数关闭它时。我收到一个错误,说 unfined 当 dismiss 函数 被称为。

import {NgbModal, ModalDismissReasons,NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { ModalSizes } from 'src/app/core/utilities/modal-sizes';
import { timer } from 'rxjs';
declare var window: any;

export class UploadComponent {
 
 @ViewChild("spinnerLoaderModal") content;
  loading = false;
  spinnerDuration = 2000;
  closeResult: string;
  //modalReference: NgbModalRef;
  modalReference: NgbModalRef;
 
 constructor(
   private cdRef: ChangeDetectorRef,
   private modalService: NgbModal,){}


   //this.modalService.open(this.content, ModalSizes.lg);
   
 open(content){
    this.modalReference = this.modalService.open(content);
    this.modalReference.result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed `;
    });
    //this.modalReference.close();
    }

 public closeSpinnerModal()
 {

  this.modalReference.dismiss(); 
 }
 
 public onSubmit(event : MouseEvent) {
 this.isLoading = true;
   this.open(this.content)
   timer(this.spinnerDuration).subscribe(this.closeSpinnerModal)
 }

}

[HTML全文]

<ng-template #spinnerLoaderModal let-c="close" let-d="dismiss">
<div class="modal-body">
      <div class="row">
        <input type="hidden" id="spinLoader" class="form-control">
       </div>
            <img src="../../../../assets/images/loaderBlue.gif" id="bg" alt="" style="width:30%; margin-left: 0; margin-right: 0;">
  </div>
</ng-template>
javascript angular typescript twitter-bootstrap

评论

0赞 Sree Kumar 11/3/2023
我不是 Angular 开发人员,所以常识性问题:您可以尝试将 更改为 吗?当您以现在的方式调用该方法时,该方法可能就在那里执行。timer(..).subscribe(this.closeSpinnerModal)timer(..).subscribe( val =>this.closeSpinnerModal() )

答:

0赞 Eliseo 11/3/2023 #1

您需要在订阅中使用“扁平箭头”,因此,范围将是组件

timer(1000).subscribe(()=>this.closeSpinnerModal())

注1:没有“let-d dismiss”,

在您使用的模板中

d.close('what ever') //to close and fall into "then(result)"
d.dismiss('what ever other')  //to close and fall into the (reason)

注2:使用

<img src="./assets/images/loaderBlue.gif">
//or
<img src="assets/images/loaderBlue.gif">

Rememeber,Angular创建一个目录,在这个目录中有index.html和你的assets文件夹的副本。Angular 从这个文件夹中寻找“图像”