1

多くのコンポーネントを持つ典型的な角度2のアプリケーションがあります。
私はこのモーダルコンポーネントをインストールしました:https://github.com/pleerock/ng2-modal
より多くのコンポーネント間で同じモーダルを共有する方法はありますか? 私はよく説明します。私は同じモダールを別のコンポーネント内の異なるボタンをクリックして開く必要があります。出来ますか?コンポーネント間で角の2つのモーダルを共有する

私はこのようなアプローチを試みましたが、常にモーダルにコンテンツを追加するので、正しい方法ではありません。

私は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のオリジナルバージョンは、実際に私の質問であることを

+0

あなたは何を試してみましたか?それを参照して、動作していないコードを投稿して、ヘルプを入手しようとしてください。あなたが何かを試して、修正を必要とするとき、誰かにあなたのためにそれをやるように求めるのではなく、人々はより多くの助けを得て応答します。 –

+0

さて、私はしました。とにかく、私が試したアプローチは、私が必要なものに合っていたので、投稿するのが役に立つとは思わなかった。私は昨日の午後全体を挑戦しました、それは怠惰でした。 –

+1

そのプランカーのオリジナルバージョンは実際に私の質問です:http://stackoverflow.com/questions/36566698/how-to-dynamically-create-bootstrap-modals-as-angular2-componentsと@Günterは私が思うに素晴らしい答えを出しました本当に過小評価されている。ちょっと間違いがありますが、私はGünterに説明しますが、ここで固定バージョンを見つけることができます:https://plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview – echonax

答えて

2

:ギュンター@How to dynamically create bootstrap modals as Angular2 components?

は、私は本当に過小評価だと思う素晴らしい答えをしました。

plunkerは小さなミスがあり、あなたはここで修正版を見つけることができます。https://plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview

ザ・ケースは、間違ったフィールドを参照するので、変更された場合

if(this.cmp) { 
    this.cmp.destroy(); 
} 

if(this.cmpRef) { 
    this.cmpRef.destroy(); 
} 
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ビルドで問題を修正するためにコンポーネントを更新しました。移動するとリポジトリのリンクも編集されました;) –

関連する問題