Angular 2:如何创建删除服务但只是将状态更新为非活动
Angular 2: How to create Delete service but just updating the status as inactive
顺便说一下,我是 Angular 的新手。
我有一个删除按钮,它会向用户显示一个确认对话框,告知用户是否真的要删除员工。当用户选择 YES 时,前端会将 employeeID 传递给后端,服务应该只更新员工和 return FALSE 或 TRUE 给前端。
Q: What should be the correct content in my deleteEmployee()
in service (Observable)?
我的服务文件中有这些代码
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);
}
}
还有这个:
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);
}
函数 return 仅限布尔值 例如: true
OR false
handleError(): boolean {
let res: boolean;
if (true) {
return true;
} else {
return false;
}
}
顺便说一下,我是 Angular 的新手。
我有一个删除按钮,它会向用户显示一个确认对话框,告知用户是否真的要删除员工。当用户选择 YES 时,前端会将 employeeID 传递给后端,服务应该只更新员工和 return FALSE 或 TRUE 给前端。
Q: What should be the correct content in my
deleteEmployee()
in service (Observable)?
我的服务文件中有这些代码
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);
}
}
还有这个:
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);
}
函数 return 仅限布尔值 例如: true
OR false
handleError(): boolean {
let res: boolean;
if (true) {
return true;
} else {
return false;
}
}