I am new to Angular, by the way.
I have a delete button that shows a confirmation dialog to the user on whether to really delete an employee or not. When user chooses YES, frontend would pass the employeeID to the backend and the service should just update the employee and return a FALSE or TRUE to the frontend.
Q: What should be the correct content in my
deleteEmployee()
in service (Observable)?
I have these codes in my service file
getEmployees();
getEmployees(filter?: any): Observable<Employee[]> {
if (typeof filter !== 'undefined') {
let body = JSON.stringify({filter});
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
console.log(body);
return this.http.post('http://52.193.131.36:8000/api/staffs', body, options)
.map(this.extractData)
.catch(this.handleError);
} else {
return this.http.get('http://52.193.131.36:8000/api/staffs')
.map(this.extractData)
.catch(this.handleError);
}
}
And this:
private extractData(res: Response | any) {
let body = res.json();
return body.data || {};
}
private handleError (error: Response | any) {
// In a real world app, you might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}