【发布时间】:2017-05-06 00:29:55
【问题描述】:
我有一个包含许多组件的典型 Angular 2 应用程序。
我安装了这个模态组件:https://github.com/pleerock/ng2-modal.
有没有办法在更多组件之间共享相同的模态?
我解释得更好。我希望单击不同组件内的不同按钮时应该打开相同的模式。可能吗?
我尝试过这种方法 https://plnkr.co/edit/RgI1e9PobC7FloLHpEFD?p=preview 但这不是正确的方法,因为它总是向模态框添加内容。
我把它贴在我的 app.ts 文件下面:
21/12/2016 更新
根据@echonax 的建议,我更新了我的 plunk:
//our root app component
import {
NgModule,
ComponentRef,
Injectable,
Component,
Injector,
ViewContainerRef,
ViewChild, ComponentFactoryResolver} from "@angular/core";
import {BrowserModule} from '@angular/platform-browser'
import {Subject} from 'rxjs/Subject';
@Injectable()
export class SharedService {
showModal:Subject<any> = new Subject();
}
@Component({
selector: 'child-component',
template: `
<button (click)="showDialog()">show modal from child</button>
`,
})
export class ChildComponent {
constructor(private sharedService:SharedService) {}
showDialog() {
this.sharedService.showModal.next(CompComponent);
}
}
@Component({
selector: 'comp-comp',
template: `MyComponent`
})
export class CompComponent { }
@Component({
selector: 'modal-comp',
template: `
<div class="modal fade" id="theModal" tabindex="-1" role="dialog" aria-labelledby="theModalLabel">
<div class="modal-dialog largeWidth" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="theModalLabel">The Label</h4></div>
<div class="modal-body" #theBody>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" (close)="close()">Close</button>
</div></div></div></div>
`
})
export class ModalComponent {
@ViewChild('theBody', {read: ViewContainerRef}) theBody;
cmp:ComponentRef<any>;
cmpRef:ComponentRef<any>;
constructor(
sharedService:SharedService,
private componentFactoryResolver: ComponentFactoryResolver,
injector: Injector) {
sharedService.showModal.subscribe(type => {
if(this.cmp) {
this.cmp.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(type);
this.cmpRef = this.theBody.createComponent(factory)
$('#theModal').modal('show');
});
}
close() {
if(this.cmp) {
this.cmp.destroy();
}
this.cmp = null;
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello</h2>
<button (click)="showDialog()">show modal</button>
<child-component></child-component>
</div>
`,
})
export class App {
constructor(private sharedService:SharedService) {}
showDialog() {
this.sharedService.showModal.next(CompComponent);
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, ModalComponent, CompComponent, ChildComponent],
providers: [SharedService],
entryComponents: [CompComponent],
bootstrap: [ App, ModalComponent ]
})
export class AppModule{}
敬请期待!
【问题讨论】:
-
你试过什么?尝试引用它,并发布不起作用的代码,以尝试获得帮助。当您尝试过某事并且只需要修复时,人们会给予更多帮助,而不是要求别人为您做这件事。
-
好的,我做到了。无论如何,我尝试过的任何方法都适合我的需要,所以我认为发布它们没有用。昨天下午试了整个,不是偷懒。
-
那个 plunker 的原始版本实际上是我的问题:stackoverflow.com/questions/36566698/… 和 @Günter 给出了一个惊人的答案,我认为这真的被低估了。它有一个小错误,我会告诉 Günter 但你可以在这里找到修复的版本:plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview
-
@echonax 非常感谢!我尝试在我的代码中实现它,我会告诉你的。
-
好吧,因为 SharedService 服务必须写成这样:
showModal:Subject<any> = new Subject();;)
标签: angular data-binding modal-dialog bootstrap-modal sharing