我正在尝试制作一个可以从同一个模态本身调用的可重用模态组件。
我如何配置组件和模式,以便当可重用组件打开时,旧实例将直接关闭?
下面是我的堆栈闪电战。
https://stackblitz.com/edit/angular-nested-component-modal-ngx-bootstrap-n
我正在尝试制作一个可以从同一个模态本身调用的可重用模态组件。
我如何配置组件和模式,以便当可重用组件打开时,旧实例将直接关闭?
下面是我的堆栈闪电战。
https://stackblitz.com/edit/angular-nested-component-modal-ngx-bootstrap-n
我会使用一种模态...
这是策略
让我们从定义我们的服务开始
import { Injectable } from "@angular/core";
import { BehaviorSubject, Subject } from "rxjs";
interface IModalParams {
component: any;
config?: any;
title?: any;
}
@Injectable()
export class MyService {
modalOpen$ = new BehaviorSubject(false);
component;
config;
title;
show({ component, config, title }: IModalParams) {
this.component = component;
this.config = config;
this.title = title;
this.modalOpen$.next(true);
}
}
所以我们定义了一个 show 方法来设置一些变量(component
,configuration
和title
)
我们还定义了一个主题modalOpen$
。现在,当用户打开新模式时,将通知订阅此属性的任何属性
ngOnInit() {
this.myService.modalOpen$.pipe(
tap(() => this.modalRef?.hide()),
filter(isOpen => isOpen)
).subscribe({
next: () => {
const {component, config, title} = this.myService
this.modalRef = this.modalService.show(component, config);
if(title) {
this.modalRef.content.title = title;
}
}
})
}
这里我们订阅modalOpen$
并打开或关闭提供的组件
this.myService.show({
component: ModalContentComponent,
config: {
ignoreBackdropClick: false
},
title: "Modal with component"
})
}
在其他组件中,我们现在可以使用show
上面指定的方法