Ngx-Bootstrap 如果从订阅中显示,模态不刷新数据更改

Ngx-Bootstrap modal not refreshing data changes if shown from subscription

我正在尝试从组件打开一个模式,并且我正在调用 show 方法来响应另一个订阅,如果在订阅之外调用它,它工作正常。但是从订阅中,传递给内容的数据没有被刷新下面是代码示例。

//Subscription
$subject.subscribe(res => {
  const bs_modal = this._bsModalService.show(ComponentA);
  bs_modal.content.data = res;

  this._bsModalService.onHide.subscribe(() => {
     console.log(bs_modal.content.res_data));
  })

})

// Modal Component
@Component({
  selector: 'app-modal',
  template: `
     <div>{{data | json}}<div>
     <button (click)="hideModal()">Hide Me</button>
  `,
})

class ComponentA {
  data: any;
  res_data: any;
  constructor(_bsModalRef: BsModalRef) {}

  hideModal() {
    this.res_data = 'Testing';
    this._bsModalRef.hide();
  }
}

在模态打开时 data 是空的,每当我关闭模态时 res_data 也是未定义的

我已经在 stackblitz 中重新创建了您的代码,并且在调整了一些东西后我能够将数据传递到模态中:https://stackblitz.com/edit/angular-37iw9w

确保这些事情:

  • 您已经导出组件A class。
  • 您已在模块中导入 ModalModule.forRoot()。
  • 您已将 componentA 添加到模块中的声明数组。
  • 您已将 componentA 添加到模块中的 entryComponents 数组。

这是基于您提供的代码示例的假设,它们属于一个模块。