Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventEmitter emit is not working in angular 4

Tags:

angular

emit

here is my code below :

search.component.html

<button (click)="addMe()">Click</button>

search.component.ts

import { Component, Directive, OnInit, Input, Output, EventEmitter } from '@angular/core';

@Component({
   selector: 'search-component',
   templateUrl: './search.component.html'
})

export class SearchComponent {
   @Output() userUpdated = new EventEmitter();

   addMe() {
       this.userUpdated.emit('my data to emit');
   }
}

profile.component.html

<search-component (userUpdated)="handleUserUpdated($event)"></search-component>

profile.component.ts

handleUserUpdated(e) {
   console.log('e', e);
}
like image 392
Chandru Avatar asked Sep 05 '17 07:09

Chandru


1 Answers

You should need to declare a type. Use @Output() userUpdated = new EventEmitter<string>(); if you want it to be a string or @Output() userUpdated = new EventEmitter<any>(); if it can be any type.

Also, you need to change your console log, try swapping to console.log("e-" + e)

like image 150
mast3rd3mon Avatar answered Sep 20 '22 11:09

mast3rd3mon