I am working on implementing ecommerce website in Angular4.Here I have a checkout page where I have to display individual as well as cart products based on the user coming from the previous state.My problem is that I need to get the state name from which user has navigated to the current page,for this purpose I am checking a condition. I am stuck at getting the state name in Angular4 . How can I get the previous state name in Angular4?
Asked
Active
Viewed 574 times
1
-
In angular app state is maintained by routing. You can define a specific routing for each state. However, we need more detail about your specific problem. – asmmahmud Oct 09 '17 at 12:27
-
I just want to get the previous state name after navigation – srujana Oct 09 '17 at 12:30
-
Just put in as a value in your route check: https://stackoverflow.com/questions/36835123/how-do-i-pass-data-in-angular-2-components-while-using-routing – Swoox Oct 09 '17 at 12:33
1 Answers
0
You can use a service.
One of the uses of services is to store data. This data persists across component changes and routing and others.
Just generate a service (maybe using angular-cli: ng g service your-service-name
. Add a variable to that service and call it, for example, "state".
import { Injectable } from '@angular/core';
@Injectable()
export class YourService
{
state: String = '';
constructor()
{
}
}
then, in your component files, you just import it in your components
import { YourService } from '../../path/to/your.service';
and in the constructor:
constructor(
private yourService:YourService
)
{}
and anywhere in the code you can do
this.yourService.state = "something";
and do your checks and so on

steoiatsl
- 1,892
- 2
- 22
- 39
-
-
just make an enumeration of your states. Say "visitor", "loggedIn", "checkout", "transactionSuccess", "transactionFailure", ...any strings. You can add more variables other than state. – steoiatsl Oct 09 '17 at 13:36