I am a beginner for learning Angular 2 type script. I want to pass the data between one component to another component?
Serives.ts
import {Component, Injectable} from '@angular/core';
// Name Service
export interface myData {
myDataname:any;
}
@Injectable()
export class GetService {
sharingData:any={myDataname:"My data string"};
saveData(str:any){
alert("Save Data");
// console.log('save data function called' + JSON.stringify(str));
this.sharingData.myDataname=str;
console.log(JSON.stringify(this.sharingData.myDataname));
}
getData():any{
console.log('get data function called');
console.log(JSON.stringify(this.sharingData.myDataname)+ "Get dats service s");
return this.sharingData.myDataname;
}
}
First Component.ts
import { GetService } from '../service/get-service';
@Component({
selector: 'firstcomponent,
templateUrl: 'firstcomponent.html',
providers:[GetService]
})
export class firstcomponentDetails {
firstname:string;
lastname:string;
constructor(public getservice: GetService ) {
this.getservice= getservice;
submitForm(value: any):void{
console.log(' Personal Details Form submited!');
console.log(value);
this.getservice.saveData(value);
}
}
}
Second Component.ts
import { GetService } from '../service/get-service';
@Component({
selector: 'secondpage',
templateUrl: 'secondpage.html',
providers: [GetService],
})
export class SecondPage {
getDatavaluesServices:any;
constructor( public getservice:GetService){
this.getservice = getservice;
this.getDatavaluesServices=getservice.getData();
console.log(this.getDatavaluesServices );
}
}
This is my code. I tried this code passing data first component to second component. I am getting to string value my data string
. How to get first component value?