4

I want the child component to emit two different events in it`s parent Component, depending on which button the user clicks. The first one (onSelectedKat) works fine. But the second one (onAllClick) doesn´t work. It has the same structure like the first one, I just changed the names.

I tried to skip all the unnecessary code, so it doesn´t get too confusing.

This is the html code of the child. It has two buttons, which call the two different functions on click. These functions in turn, call the emitter-functions.

<a (click)="selectKat(kategorie.name)" class="btn btn-default btn-kat" role="button">{{kategorie.name}}</a>

<a (click)="clickAll()" class="btn btn-default btn-kat" role="button">Alle</a>

This is the code of the child. Here it gets stuck in the clickAll() function. The console.log works, but everything after that doesn`t.

import { EintraegeComponent } from '../eintraege/eintraege.component';

@Component({
  selector: 'app-kategorien',
  templateUrl: './kategorien.component.html',
  styleUrls: ['./kategorien.component.css'],

})
export class KategorienComponent implements OnInit {

  @Output() onSelectedKat = new EventEmitter<string>();
  @Output() onAllClick = new EventEmitter<any>();

  constructor() {}

  ngOnInit() {}

  selectKat(name: string){
    this.onSelectedKat.emit(name);
  }

  clickAll(){
    console.log("clicked in child works");
    this.onAllClick.emit();
  }

}

And this is the code of the parent. However the onAllClick() in that one is never called.

@Component({
  selector: 'app-eintraege',
  templateUrl: './eintraege.component.html',
  styleUrls: ['./eintraege.component.css']
})
export class EintraegeComponent implements OnInit {

  eintraegegeordnet:boolean;
  toFilter:string;

  constructor() { 
      this.eintraegegeordnet = false;
    }

  ngOnInit() {}

  onAllClick(){
    console.log("Click in parent works");
    this.toFilter = "boo";
    this.eintraegegeordnet = false;
  }

  onSelectedKat(name:string){
    this.eintraegegeordnet = true;
    this.toFilter = name;
  }   

}

Can anyone tell me what I´m doing wrong and why one of them works and the other doesn`t?

Sreekumar P
  • 5,900
  • 11
  • 57
  • 82
Tobsi
  • 51
  • 1
  • 5
  • 1
    can you add the parent HTML in which it listen to onAllClick? plus try to remove from the EventEmitter, because in this case it expect to data. – Raed Khalaf Jul 11 '17 at 14:40
  • ohh I see what you mean, I totally forget to add the function in the html. Thanks for the hint, I just try to add it and look if it works! – Tobsi Jul 11 '17 at 14:47

1 Answers1

1

Thanks for the Hint from Raed Khalaf, it now works. I forgot to add the function in the Parent HTML.

<app-kategorien (onSelectedKat)="onSelectedKat($event)" (onAllClick)="onAllClick($event)"></app-kategorien>
Tobsi
  • 51
  • 1
  • 5