I am passing data between two components using service
UserComponent
onSubmit(username:string){
this.dataService.push(this.user);
this.navigate();
}
navigate(){
this.router.navigate(['question']);
}
Question Component
import { Component, Input, ViewChild, ElementRef, Renderer } from '@angular/core';
import { DataService } from '.././services/data.service';
@Component({
moduleId: module.id,
selector: 'question',
templateUrl: 'question.component.html',
})
export class QuestionComponent {
name : string;
constructor(private dataService: DataService, private renderer: Renderer) {
}
ngOnInit() {
console.log("Here");
this.dataService.pushData.subscribe((data:any) => this.name = data);
}
Service
import { Injectable,EventEmitter } from '@angular/core';
@Injectable()
export class DataService{
pushData = new EventEmitter<string>();
push(value:string){
console.log("In Service");
this.pushData.emit(value);
}
}
}
But i am able able to get the data in the question component and no error in console