Reputation: 23
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'
Upvotes: 1
Views: 159
Reputation: 104890
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 { }
Upvotes: 1