How to listen for angular 2 MANUALLY events on a nested dependency instance?

If I have a component like

@Component({selector: 'todo-cmp'})
class TodoCmp {
  @Input() model;
  @Output() complete = new EventEmitter(); // TypeScript supports initializing fields

  onCompletedButton() {
    this.complete.next(); // this fires an event
  }
}

and in another component I get a copy of it through DI, as in:

...
 class SomeOtherClass(){
    constructor(todoCmp:TodoCmp){
   // how do I listen to 
   ...
 }
 ...

How to add an event listener manually inside "SomeOtherClass" and listen to any click events fired from the instance entered in Dependendcy ToDoCmp ..

something like todoCmp.addEventListener ('complete', function (e) {});

may be? or is something better in ng2?

TX

Sean.

+4
source share
1 answer

First, there EventEmitter.next()was EventEmitter.emit(), since alpha 45 or so.

Secondly, the method you are looking for, .subscribe()

class SomeOtherClass(){
  constructor(todoCmp:TodoCmp){
     todoCmp.complete.subscribe((result)=>{
        //result == arg passed into emit()  
     }))
 }

, - , EventEmitter , .

+7

Source: https://habr.com/ru/post/1621191/


All Articles