2017-01-13 3 views
2

にイベントを放出するためにどのように私は動的にこのようにして、コンポーネントから作成されたモーダルがあります。動的にロードされたモーダルから親

@Injectable() 
export class SharedService { 
    showModal:Subject<any> = new Subject(); 
} 

@Component({ 
    selector: 'comp-comp', 
    template: `MyComponent dataToPass: {{dataToPass}}, dataToPass2: {{dataToPass2}} 

      <button (click)="updateData()">Update data</button>` 
}) 
export class CompComponent { 
    @Output() setupDataUpdated = new EventEmitter();  
    private dataToPass2; 

    constructor() {} 

    ngAfterContentInit() { 
    this.dataToPass2 = this.dataToPass + ' hello'; 
    } 

    updateData() { 
    console.log('data updated'); 
    this.setupDataUpdated.emit('emitted_value'); 
    } 
} 

@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">{{theHeader}}</h4></div> 
     <div class="modal-body" #theBody (setupDataUpdated)="updateSetupData($event)"> 
     </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; 

    theHeader: string = ''; 
    dataToPass: string = ''; 
    cmpRefBody:ComponentRef<any>; 

    constructor(
    sharedService:SharedService, 
    private componentFactoryResolver: ComponentFactoryResolver, 
    injector: Injector) { 

    sharedService.showModal.subscribe(data => { 
     if(this.cmpRef) { 
     this.cmpRef.destroy(); 
     } 
     let factory = this.componentFactoryResolver.resolveComponentFactory(data.type); 
     this.cmpRef = this.theBody.createComponent(factory); 
     this.cmpRef.instance.dataToPass = data.dataToPass; 
     this.dataToPass = data.dataToPass; 
     this.theHeader = data.title; 
     console.log(data.title); 
     console.log(data.dataToPass); 
     $('#theModal').modal('show'); 
    }); 
    } 

    close() { 
    if(this.cmpRef) { 
     this.cmpRef.destroy(); 
    } 
    this.cmpRef = null; 
    } 

    updateSetupData(data) { 
     console.log('update data'); 
     console.log(data); 
    } 

} 

@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({'type': CompComponent, 'title': 'titolo1', 'dataToPass': 'dataToPass'}); 
    } 
} 

(参照元:Angular 2 modals shared between componentsを)。

ご覧のとおり、私はイベントをモーダルの子コンポーネントから発行しようとしていましたが、モーダルの親コンポーネントはイベントを表示していないようです。 あなたはその理由を知っていますか?私のミスはどこですか?

+0

。 – isherwood

答えて

2

は、手動で以下のようにあなたのEventEmitterに加入する必要があります。答えを受け入れるか、この疑問を解決することができるようにコメントを残してください

this.cmpRef.instance.setupDataUpdated.subscribe((data) => this.updateSetupData(data)); 

Modified Plunker

+0

これは適切なアプローチのようですが、逆に実装されていると思います。モーダルコンポーネントにサブスクライバを追加しました。これはモーダルを呼び出すコンポーネントになければなりません。また、プランカは壊れていた。 – isherwood

+0

@isherwood Fixed plunker – yurzui

関連する問題