I am performing edit operation inside Angular material dialog.I am able to send the data(id and data-key) to dialog.In dialog I'm binding the data that is sent by the component.After binding now I need to edit that prefilled data.I am not able to edit since I don't know how to pass id in API.
Code :
Component.ts :
openModal(id : number, status : string): void {
const dialogRef = this.dialog.open(UpdateRowComponent,
{
data :{
'id' :id,
'status': status }
});
dialogRef.afterClosed().subscribe((result:string) => {
console.log('The dialog was closed');
console.log(result);
});
}
Dialog.component.ts : (Not using services)
userId:string = "";
id:number;
Data:object = {};
spf = [];
exist = false;
productObj:object = {};
private headers = new Headers({ 'Content-Type': ' application/json'});
@Input() item: any;
constructor(private http: Http, public dialogRef :MatDialogRef<UpdateRowComponent >,private dialog: DialogService,private router:Router,private route:ActivatedRoute,
@Inject(MAT_DIALOG_DATA) public data: any) {
console.log("Outside the subscriber",this.data);
}
ngOnInit() {
console.log("Inside update component",this.data);
this.http.get("http://localhost:4000/Table/").subscribe(
(res: Response) => {
this.spf = res.json();
for(var i = 0; i < this.spf.length ; i++) {
if(parseInt(this.spf[i].id) === this.id) {
this.exist = true;
this.Data = this.spf[i];
break;
} else {
this.exist = false;
}
}
}
)
}
This is where I'm facing problem(I can't pass id in url so what should I do here ?): dialog.component.ts:
update(data) {
this.productObj = {
"status": data.status
};
const url = `${"http://localhost:4000/Table/"}/${this.id}`;
this.http.put(url, JSON.stringify(this.productObj), {headers: this.headers})
.toPromise()
.then(() => {
console.log("Updated or ", this.data.status);
})
}