I have to 2 component LeftPanelComponent and RightPanelComponent. I referred this code for my implementation
I created one shared service between two component to call abc() function of RightPanelComponent from LeftPanelComponent.
I'm reading value from ListDialogComponent into LeftPanelComponent and passing to abc() function.
abc() function of RightPanelComponent is calling successfully from LeftPanelComponent.
But when I'm calling saveCustomData() angular 2 service in abc() function then I'm getting following error.
EXCEPTION: Error in ./ListDialogComponent class ListDialogComponent - inline template:29:6 caused by: Cannot read property 'saveCustomData' of undefined
LeftPanelComponent.ts
import { Component, OnInit } from '@angular/core';
import {LeftpanelService} from "../leftpanel.service"
import {Leftpanel} from "../leftpanel";
import {MdDialog} from "@angular/material";
import {MdDialogRef} from "@angular/material";
import {ListDialogComponent} from "../list-dialog/list-dialog.component";
import {SharedService} from '../shared-service/shared-service.component';
@Component({
selector: 'app-leftpanel',
templateUrl: './leftpanel.component.html',
styleUrls: ['./leftpanel.component.css']
})
export class LeftpanelComponent implements OnInit {
dialogRef: MdDialogRef<any>;
leftpanels:Leftpanel[];
constructor(public dialog: MdDialog,private service:LeftpanelService, private sharedService: SharedService) {
}
ngOnInit() {
this.service.getLeftpanels().subscribe(lst =>this.leftpanels=lst);
}
transferDataSuccess($event) {
this.receivedData.push($event.dragData);
this.openDialog();
}
openDialog() {
this.dialogRef.afterClosed().subscribe(result => {
console.log('result: ' + result);
this.dialogRef = null;
this.sharedService.componentOneFn(sessionStorage.getItem("value"));
});
}
}
ListDialog.Component.html
<form class="form-group" >
<select name="itemid" #selectedCategory class="selectOption">
<option value="">Choose Item</option>
<option *ngFor="let Item of Items" value="{{Item.id}}" >{{Item.name}}</option>
</select>
<p></p>
<button type="button" (click)="dialogRef.close('yes',getValueFromSelect(selectedCategory.value))" class="btn">Submit</button>
<button type="button" (click)="dialogRef.close('no')" class="btn">Cancel</button>
</form>
ListDialogComponent.ts
import { Component,OnInit} from '@angular/core';
import {MdDialogRef} from "@angular/material";
import {Item} from '../item';
import {GetListService} from '../get-list.service';
@Component({
selector: 'app-list-dialog',
templateUrl: './list-dialog.component.html',
styleUrls: ['./list-dialog.component.css']
})
export class ListDialogComponent implements OnInit {
Items : Item[];
serverItemID : string;
constructor(public dialogRef: MdDialogRef<ListDialogComponent>,private service:GetListService) { }
ngOnInit() {
this.service.getItemList(this.oauthTokenService.getHeaders()).subscribe(lst =>this.Item=slst);
}
getValueFromSelect(value){
sessionStorage.setItem('value',value);
}
}
RightPanelComponent.ts
import {SaveCustomItem} from '../save-custom-item';
import {SaveCustomService} from '../save-custom.service';
import {SharedService} from '../shared-service/shared-service.component';
@Component({
selector: 'app-rightpanel',
templateUrl: './rightpanel.component.html',
styleUrls: ['./rightpanel.component.css']
})
export class RightpanelComponent implements OnInit {
componentData = [];
componentData2 = [];
saveCustomItems:saveCustomItem[];
constructor(public dialog: MdDialog, private service:SaveCustomService, private sharedService: SharedService) {
this.sharedService.componentOneFn = this.abc;
}
abc(value) {
if(value == "MyValue") {
this.service.saveCustomData(value).subscribe(lst =>this.saveCustomItems=lst);
}
}
}
SharedService.ts
import {Injectable} from '@angular/core';
@Injectable()
export class SharedService {
componentOneFn: Function;
constructor() { }
}
SaveCustomService.ts
import { Injectable } from '@angular/core';
import {Http, Response, Headers} from "@angular/http";
import {Observable} from "rxjs/Rx";
import 'rxjs/Rx';
import {AppSettings} from './appsettings';
import {SaveCustomItem} from './save-custom--item';
@Injectable()
export class SaveCustomService {
constructor(private http:Http) { }
saveCustomData(value):Observable<SaveCustomItem[]> {
let response = this.http.get(`${AppSettings.BACK_ENDPOINT}/ajax/save-custom-data?value=`+value);
let saveCustomItems = response.map(mapSaveCustomData);
return saveCustomItems;
}
}
function mapSaveCustomData(response:Response):SaveCustomItem[] {
return response.json().results.map(toSaveCustomData);
}
function toSaveCustomData(r:any):SaveCustomItem {
let saveCustomeItem = <SaveCustomItem>({
id:r.server,
title:r.title
});
return saveCustomeItem;
}