저는 Angular 2 형 스크립트를 배우기위한 초보자입니다. 하나의 구성 요소와 다른 구성 요소간에 데이터를 전달하고 싶습니다.각도 2 유형 스크립트에서 탐색 또는 경로없이 데이터 하나의 구성 요소를 다른 구성 요소에 전달할 수있는 모든 방법은 무엇입니까?
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;
}
}
먼저 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);
}
}
}
둘째 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);
}
}
이것은 내 코드. 이 코드는 첫 번째 구성 요소를 두 번째 구성 요소로 전달했습니다. 문자열 값 my data string
에 도착했습니다. 첫 번째 구성 요소 값을 얻는 방법?
https://angular.io/docs/ts/latest/cookbook/component-communication.html –
http://stackoverflow.com/questions/42624053/angular-2-how-interact-between-different-level -components/42624414 # 42624414 여기 이미 대답했습니다. –