1

저는 많은 구성 요소가있는 전형적인 앵귤러 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 내가 생각하는 놀라운 대답을 만들었습니다 정말 과소 평가됐다. 작은 실수가 있지만 Günter에게 알려 주겠다. https://plnkr.co/edit/oMCZIJq58WdLgYf2D0Di?p=preview – echonax

답변

2

plunker의 원래 버전이 실제로 내 질문하는지 : 귄터 @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 빌드시 문제를 해결하기 위해 구성 요소를 업데이트했습니다. 이동하기 때문에 Repo의 링크도 편집했습니다.) –