Search code examples
javascriptangulartypescriptionic-frameworkngmodel

Loosing data upon navigation, page change


My app displays a profile name that can be edited. When i edit it it changes, great. When i navigate and come back, the data reset's.

I've tried holding the data (profile name) as a String variable, I've tried holding the data as a list and displaying the name with String[0] to display the unshifted profile name.

First go

<h3>{{profileService.profileName}}'s Profile</h3>

this.profileService.changeName(this.pName);

changeName(data){
    this.profileName = data;
}

Second go

<h3>{{profileService.profileNames[0]}}'s Profile</h3>

this.profileService.changeName(this.pName);

changeName(data){
    this.profileNames.unshift(data);
}

So, again, this updates initially when I go from edit-page to home-page. When I go to another page and return, the updated profile name is MISSING. Thanks!


Solution

  • Have you made sure that:

    • The service is provided for both the home page and the edit page?

    This can be done by: in app.module.ts (I guess you want your changes to persist for the whole application)

    @NgModule({
      declarations:[
      //your components here
      ],
      imports:[
      //your modules here 
      ],
      providers:[
      ProfileService //and other services
      ]
    })
    

    or in profile.service.ts

    import { Injectable } from '@angular/core';
    @Injectable({
      providedIn: 'root'// which means there will be a single instance of the service in the whole application
    })
    export class ProfileService {
    }