2016-12-20 85 views
1

我有一個典型的Angular 2應用程序與許多組件。
我安裝了這個模態組件:https://github.com/pleerock/ng2-modal
有沒有辦法在更多的組件之間共享相同的模態? 我解釋得更好。我希望同樣的模式應該打開點擊不同組件內的不同按鈕。可能嗎?組件之間共享的角度2模式

我試過這種方法 https://plnkr.co/edit/RgI1e9PobC7FloLHpEFD?p=preview 但它不是正確的方法,因爲它總是添加內容到模態。

我張貼在這裏,我下面的app.ts文件:

21/12/2016更新
繼@echonax建議,我更新了我的普拉克:

//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{} 

敬請期待!

+0

你嘗試過什麼?嘗試引用它,併發布不工作的代碼,嘗試獲得幫助。當你嘗試過某些東西時,人們會以更多的幫助作出迴應,只是需要修復,而不是要求某人爲你做。 –

+0

好的,我做到了。無論如何,我嘗試的任何方法都適合我所需要的,所以我認爲發佈它們並不是很有用。我昨天下午整個試了一下,這不是懶惰。 –

+1

這個plunker的原始版本實際上是我的問題:http://stackoverflow.com/questions/36566698/how-to-dynamically-create-bootstrap-modals-as-angular2-components和@Günter作出了一個驚人的答案,我認爲是真的被低估了。它有一個小錯誤,我會告訴岡特有關,但你可以在這裏找到固定的版本:https://plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview – echonax

回答

1

我知道這個話題已經有6個月的時間了,但對於我來說,實現這樣的目標確實是一筆交易。

所以我製作了一個npm模塊,允許共享數據和遠程打開/關閉/事件的組件之間進行模態共享。

隨時檢查出來:https://github.com/biig-io/ngx-smart-modal

演示是在這裏:https://biig-io.github.io/ngx-smart-modal/

與角兼容> = 2.0.0

+1

太謝謝你了!我會嘗試將它用於我的以下腳本。 –

+0

很高興!隨意創造一些問題,幫助我提高它;) –

+0

更新:我更新的組件固定在AOT構建的問題。我也編輯回購的鏈接,因爲它移動;) –