1

I am having problems with what looks to be the Angular router reloading the component in <router-outlet></router-outlet> and causing undesired results. Does anyone have any ideas on how to fix this?

Plunker link: https://plnkr.co/edit/yRCzbsxE0ftzqrDeRT69?p=preview

How can I change it so the variable isn't reinitialized when alternating between link 'one' and 'two'

1
  • Save it in a service and fetch it from there every time you navigate. Commented Jul 10, 2017 at 11:45

1 Answer 1

1

You can create shared service and inject it to Parent, that service can have getter and setter and you can share the data from there... something like:

import {Injectable} from '@angular/core';

@Injectable()
export class MyService {

  public data:any;

  setData(value:any) {
    this.data = value
  }

  getData():any {
    return this.data;
  }
}

and provide in a parent, for example app.module like this:

@NgModule({
  declarations: [
    AppComponent,
    AnotherComponent,
    SecondComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [ MyService ], //<< add it in providers section and access it inside your component...
  bootstrap: [AppComponent]
})
export class AppModule { }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.