Angular 2:如何创建删除服务,但只是将状态更新为非活动状态

时间:2017-06-15 08:11:02

标签: angular service angular-promise angular-services angular-observable

顺便说一句,我是Angular的新手。

我有一个删除按钮,向用户显示是否真正删除员工的确认对话框。当用户选择YES时,前端会将employeeID传递给后端,服务应该只更新员工并向前端返回FALSE或TRUE。

  

问:我的deleteEmployee()服务中的正确内容(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);
    }

1 个答案:

答案 0 :(得分:0)

The function return boolean value only For Ex: true OR false

handleError(): boolean {
   let res: boolean;

      if (true) {
        return true;
      } else {
        return false;
      }
}