Matt Westlake
Matt Westlake

Reputation: 3651

Why does the data in My Angular Service keep getting wiped?

I have a component that uploads a file and saves the data in a service

this.csvDataService.saveCSV(csv)

Then I redirect to another service:

this.router.navigate(['/map'])

in my map component:

  ngOnInit() {
    this.getSnappedRoute()
  }

  getSnappedRoute() {
    let inputCSV = this.csvDataService.getCSV()
    this.googleMapsService.tracePathOnRoad(inputCSV).subscribe(routeLocations => {
      this.snappedRoute = routeLocations['json']['snappedPoints'];
      this.openMap();
    })
  };

  openMap() {
    let mapProp = {
      center: new google.maps.LatLng(this.snappedRoute[0]['location']['latitude'], this.snappedRoute[0]['location']['longitude']),
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.hybrid
    }

    let map = new google.maps.Map(document.getElementById("googleMap"), mapProp)
  }

This works well for the first time, however if I refresh the page, the csv data in my csvDataService is gone.

Upvotes: 1

Views: 325

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61339

Data in an Angular service is stored in memory. Refreshing the page, like restarting an application, clears that memory. There is no reason to expect application state to persist over a reload. This is true whether you are putting it in a service, a component, or any other object.

If you need it to persist, use a cookie or local storage, or send the data to a database.

Upvotes: 2

Related Questions