I want to share data between component with use @Input or @output because I'm just need to share in back-end don't need to display
Asked
Active
Viewed 200 times
0
-
1With or without? Make sure that the title and the description match. – ConnorsFan Jan 10 '20 at 04:34
-
Does this answer your question? [How to pass data between two components in Angular 2](https://stackoverflow.com/questions/39325503/how-to-pass-data-between-two-components-in-angular-2) – vighnesh153 Jan 10 '20 at 06:27
2 Answers
1
If you want to pass data between components
without @input @output
so you can user service.
In data.service.ts
you can do like this
sharedData : any;
setData(data){
this.sharedData = data;
}
getData(){
return this.sharedData
}
And in your parent.component.ts
you can set data like this
constructor(private _dataService : DataService){}
this._dataService.setData(dataToBeShared);
And in your child.component.ts
you can get that data like this
constructor(private _dataService : DataService){}
console.log(this._dataService.getData())

Fahad Hassan
- 781
- 10
- 20
0
If you want to pass data between components without @input @output so you can use service.
In share.service.ts you can do like this
search: EventEmitter < boolean > = new EventEmitter();
toggle(value:any) {
this.search.emit(value);
}
Component where you want data:
ngOnInit() {
this.service.search.subscribe(value => {
//some code here
}
}
Component from where you are sharing data:
submit():void{
this.service.toggle(somedata);
}

Mustafa Kunwa
- 821
- 1
- 9
- 19