I am using Angular 2 (TypeScript)
I have two components (actually they are two pages too). They are NOT parent and child relationship.
I want to pass a value to second component(page) when I click the link in the first component(page).
I know "URL parameters" way through router. But I don't want to show this parameter in the link. Is there any other way?
Thank you!
The canonical way is to establish an
injectable
service and inject the service into any components that need the data. Since the service is a singleton, the components will be accessing the same data. It shouldn't matter that they are on different pages. Plunker here.Thank you MarkM! It works, but it has been difficult for me because I have it all in diferent files and over last angular2 using angular cli... So I explain it here:
First of all: You need to create a file with the class you want to share with other components: data.service.ts and make it "@Injectable":
Make sure you set the created class (myService) on 'providers' only once on the app.module.ts file and import the injectable file: data.service.ts:
Wherever you want to use your shared class use the constructor to create an object from the class (this object will be unique for your components, thanks to the @Injectable decorator). Remember to import it on every file you use it!...
In my case this view sets the data of the object with a text input: input.component.ts
And in this view I just see the data that has been set on the object: content.component.ts
I hope this info is usefull!