子组件未订阅服务可观察

时间:2018-04-18 00:32:11

标签: angular angular5

嘿,伙计们对我错过的内容有何看法?基本上,父组件将触发单击以使用observable通过服务打开模态,子组件将订阅它,但似乎子方法没有触发。

<!-- language: lang-js -->

ModalService

import { Injectable,TemplateRef } from '@angular/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class ModalService {

  private modalOpenedSource = new Subject<any>();

  constructor() { }

  modalOpened$ = this.modalOpenedSource.asObservable();

  public openModal(modal: any){
    this.modalOpenedSource.next(modal);
  }
}

Parent

 constructor(private modalService: ModalService) {
   this.loadItems();
 }
 openContractFormModal() {
   this.modalService.openModal('')
 }

Child

  public subscription: Subscription;

  constructor(private modalService: ModalService) { 
    this.subscription = this.modalService.modalOpened$.subscribe(
      modal => {
        this.openModal();
      });
  }

  ngOnInit() {

  }
  openModal() {
    alert('works')
  }

0 个答案:

没有答案