javascript - 如何从动态加载的模式向父级发出事件

标签 javascript angular eventemitter

我有一个从组件动态创建的模式,这样:

@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)。

如您所见,我试图从模态子组件发出事件 setupDataUpdated,但模态父组件似乎看不到该事件。 你知道为什么吗?我的错误在哪里?

最佳答案

您需要手动订阅您的EventEmitter,如下所示:

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

Modified Plunker

关于javascript - 如何从动态加载的模式向父级发出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41640745/

相关文章:

javascript - 如何找到 HTMLAudioElement 音频结束播放

javascript - 从浏览器中的安全 token 读取数据

android - 从 ionic 4 更新到 5 ionic cordova android livereload 错误 - 复制插件

javascript - node.js eventemitter 创建多个对象还是依赖参数?

javascript - 在 ES6 类中使用 EventEmitter

events - Angular 2事件广播

Javascript 根据选中的选项计算值

javascript - JsTree 的节点未使用在 IE 中的类型插件中指定的图标

javascript - Angular 2 中的 CanActivate 返回未定义

angular - 输入超慢的 ionic 页面