rxjs 如何在 angular 服务中 [=11am observable 和一个值一起

rxjs how to return an obsevable and a value togethere in angular service

在我的 angular 应用程序中;我正在使用 ngx-bootstrap 模式。

此模态框用于 return 显示和隐藏模态框时的可观察回调 (onShown / onHidden) 点击按钮后。

我的代码如下所示:

import {Injectable, TemplateRef} from '@angular/core';
import {BsModalService} from 'ngx-bootstrap/modal';
import {BsModalRef} from 'ngx-bootstrap/modal/bs-modal-ref.service';

@Injectable()
export class MyService {

  modalRef: BsModalRef;
  constructor(private modalService: BsModalService) {

  }
  openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template ,{ class: ' modal-lg' }  );
  }

  onModalShown(){
    return this.modalService.onShown;
    // I WANT TO RETURN this.modalService.onShown + modalRef


  onModalHidden(){
    return this.modalService.onHidden;
  }
}

我的目的是如何结合 onModalShown /onModalHidden 方法,以便我可以将对模态 modalRef 的引用与 this.modalService.onShown 同时附加响应?

我想要 RETURN this.modalService.onShown + modalRef 并能够在我的组件中订阅它并获取两个数据

@Component()
export class MyComponent {

constructor( private myService : MyService) {}

 whenCallbackFired(){
  this.myService.onModalShown.subscribe(()=>{
      GET HERE BOTH INFORMATIONS ??
  })
 }

}

注意; modalRef 在点击按钮后设置

建议 ?

这样够了吗:

onModalShown() {
  return this.modalService.onShown.pipe(
    map((directive) => ({ directive, modalRef: this.modalRef })
  );
}

在您的组件中,您可以执行以下操作:

whenCallbackFired(){
  this.myService.onModalShown.subscribe(({ directive, modalRef })=> {
    console.log(modalRef);
  })
}