我可以在Angular 2中从父级发送事件到子级

raj*_*j m 16 typescript angular

我在父类中提交了按钮button in the parent component namely.

. 有很多人的课程.

每当我点击提交按钮时,我想在子类中调用一个方法.

如何使用从父到子的方法发出方法has many

从孩子到父母很容易做到.我想访问子组件的html值,因此我需要从父到子发出一个事件.

ran*_*al9 35

您可以创建一个服务,该服务在您可以定义的父组件和子组件之间共享,Observable以便您可以Observable从子组订阅该服务,并在从父级接收到某些值时执行某些操作.

//common.service.ts

import { Injectable, Inject } from '@angular/core';
import { Subject }    from 'rxjs/Subject';
@Injectable()
export class CommonService {
  private notify = new Subject<any>();
  /**
   * Observable string streams
   */
  notifyObservable$ = this.notify.asObservable();

  constructor(){}

  public notifyOther(data: any) {
    if (data) {
      this.notify.next(data);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

//parent.component.ts

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import { CommonService } from './common.service';

@Component({
  selector   : 'parent',
  templateUrl : './parent.html'
})
export class ParentComponent implements OnInit, OnDestroy {
  constructor( private commonService: CommonService ){
  }

  ngOnInit() {
    this.commonService.notifyOther({option: 'call_child', value: 'From child'});
  }
}
Run Code Online (Sandbox Code Playgroud)

//child.component.ts

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import { CommonService } from './common.service';

@Component({
  selector   : 'child',
  templateUrl : './child.html'
})
export class ChildComponent implements OnInit, OnDestroy {
  private subscription: Subscription;
  constructor( private commonService: CommonService ){
  }

  ngOnInit() {
    this.subscription = this.commonService.notifyObservable$.subscribe((res) => {
      if (res.hasOwnProperty('option') && res.option === 'call_child') {
        console.log(res.value);
        // perform your other action from here

      }
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}
Run Code Online (Sandbox Code Playgroud)


Ali*_*har 5

子组件

childComponent.ts

@Input() private uploadSuccess: EventEmitter<boolean>;
Run Code Online (Sandbox Code Playgroud)

此外,childComponent.ts订阅该活动:

ngOnInit() {
  if (this.uploadSuccess) {
    this.uploadSuccess.subscribe(data => {
      // Do something in the childComponent after parent emits the event.
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

父组件

ParentComponent.html

<app-gallery  [uploadSuccess] = "uploadSuccess" > </app-gallery>
Run Code Online (Sandbox Code Playgroud)

ParentComponent.ts

private uploadSuccess: EventEmitter<boolean> = new EventEmitter();

onImageUploadSuccess(event) {
   console.log('Image Upload succes');
   if (event.code === 'OK' && this.maxUploadLimit > 0) {
      this.galleryImagesCount = this.galleryImagesCount + 1;
      this.maxUploadLimit = this.maxUploadLimit - 1;
    }

   // The parent emits the event which was given as `@Input` variable to the child-component
   this.uploadSuccess.emit(true);
}
Run Code Online (Sandbox Code Playgroud)