0

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

2

2 Answers 2

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())
Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.