1

In one component I emit some data

@Output() search: EventEmitter<string> =
      new EventEmitter<string>(); 

onChange(searchText) {
    this.search.emit(searchText);
    console.log(searchText);
  };

How I can listen 'searchText' value in another component?

Another component isn`t child of first component.

user6403541
  • 171
  • 1
  • 1
  • 4
  • Possible duplicate of [Global Events in Angular 2](http://stackoverflow.com/questions/34700438/global-events-in-angular-2) – zpul Jun 07 '16 at 12:22

1 Answers1

2

The way you defined your output means that the parent component can be notified when corresponding events occur:

@Component({
  template: `
    <search (search)="doSomething($event)"></search>
  `,
  directives: [ SearchComponent ]
})
export class SomeComponent {
  doSomething(searchText:string) {
    (...)
  }
}

Assuming the selector of the component you described is search.

If the relation between the component that triggers the event and the one that listens is different, you should consider to use an observable within a shared service. See this doc for more details:

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360