I am facing an issue with my multi-page website that uses a router. I want to pass a variable value from one page to another.
Here is the code snippet from my contact form page:
testName:string = "hello";
ngOnInit()
{
this.dataService.Stream
.subscribe(
(city:City) => this.processData(city),
(err) => console.log('Error at processing data'),
() => console.log('Complete...')
)
}
processData(city:City)
{
console.log('the city name is: ', this.testName);
this.problematicCity = city;
this.testName = city.name;
console.log('received data for city: ', this.testName);
}
When I send a city from the main page to the data service, the console output is:
the city name is: hello
received data for city: Luxemburg
However, when I navigate to the contact page, the testName
variable reverts back to 'hello'.
I am trying to find a way to prevent this from happening. I attempted not initializing testName
, but it resulted in the error:
assignment to undeclared variable string
.
Moving testName:string = "hello";
to ngOnInit
also gives the same error.
Additional Information: This is the code for my data service:
//data.service.ts
import {Subject} from 'rxjs/Subject';
import {Injectable} from 'angular2/core';
import {City} from '../model/city.model';
@Injectable()
export class DataService
{
Stream:Subject<City>;
constructor()
{
this.Stream = new Subject();
}
}