代码之家  ›  专栏  ›  技术社区  ›  Marco24690

用户接收角度数据时如何关闭模式

  •  0
  • Marco24690  · 技术社区  · 7 年前

    因此,我在调用服务之前打开它,并希望在收到响应时关闭它。

    :

    this.loadingModalRef = this.modalService.show(LoadingModalComponent,{initialState, keyboard: false, backdrop: 'static' });
    this.myService.searchClient(data).subscribe(
      res => {
        this.loadingModalRef.hide()
      }
    );
    

    我不得不用这种方式延迟关闭它

    setTimeout(() => {
      this.loadingModalRef.hide();
     }, 200);
    

    但是当我想在收到响应时导航到另一个位置时,我遇到了一个问题。在这种情况下,模式没有被关闭,不可见,但它仍然附加到DOM中的body元素,用户不能再与接口交互。

    我怎样才能解决我的问题?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Sachin Shah    7 年前

    试着设置 this 就这样。

    var that = this;
    setTimeout(() => {
       that.loadingModalRef.hide();
    }, 200);
    

    对我来说,这对我有用。

    另一种方法是。

    import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
    
    constructor(
        public activeModal: NgbActiveModal
    ) { }
    

    closeModal() {
     this.activeModal.close('');
    }
    

    有关详细信息,请参阅此链接。 Link

        2
  •  0
  •   Chiffie    7 年前

    使用setTimeout时 this window 所以你应该用 bind

    setTimeout(() => {
       this.loadingModalRef.hide();
    }, 200).bind(this)
    

    const loadingModalRef = this.modalService;
    loadingModalRef.show(LoadingModalComponent,{initialState, keyboard: false, backdrop: 'static' });
    this.myService.searchClient(data).subscribe(
      res => {
        loadingModalRef.hide()
      }
    );
    
    推荐文章