Angular 4 - 在catch http异常中打开一个模态对话框

时间:2017-05-31 09:05:14

标签: angular ng-bootstrap

我尝试打开我的ng-bootstrap对话框,但它没有用,我也不例外......如何使用自定义模式对话框生成处理401异常的通用服务?

Service.ts

document.getElementById("circle").setAttribute("d", describeArc(150, 150, 100, 180, 360));
            function getPathArc(center, start, end, radius) {
                        end = end - 0.0001;
                        var degree = end - start;
                        degree = degree < 0 ? (degree + 360) : degree;
                        var clockWise = (degree < 180) ? 0 : 1;
                        return getPiePath(center, degreeToLocation(start, radius, center), degreeToLocation(end, radius, center), radius, clockWise);
            }
            function getPiePath(center, start, end, radius, clockWise) {
                        return 'M ' + start.x + ' ' + start.y + ' A ' + radius + ' ' + radius + ' 0 ' + clockWise + ' 1 ' + end.x + ' ' + end.y;
            };          
            function degreeToLocation(degree, radius, center) {
                    var radian = (degree * Math.PI) / 180;
                    return { 
                        'x' : Math.cos(radian) * radius + center.x,
                        'y': Math.sin(radian) * radius + center.y
                    };
            }           
            function describeArc(x, y, radius, startAngle, endAngle){
                var endAngle = endAngle - startAngle;
                startAngle = startAngle <= 0 ? (360 + startAngle) % 360 : startAngle % 360;
                endAngle = endAngle < 0 ? (360 + endAngle + startAngle) % 360 : Math.abs(endAngle + startAngle) % 360;
                    var direction = getPathArc({'x': x, 'y': y}, startAngle, endAngle, radius);
                    var d = direction;
                    return d;       
            }

this.modalService未定义,但我在 Module.ts 中声明了所有内容:

@Injectable()
export class SearchRequisitionService {
    apiURL: string;
    private modal: NgbModalRef;

    constructor(private http: Http, private modalService: NgbModal) {
        this.apiURL = 'requisition';
    }

    findRequisition(jsonSearchParams: string): Observable<Array<Reponse>> {
        let searchParams = JSON.stringify(jsonSearchParams);
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        return this.http.post(this.apiURL + '/reponse', searchParams, {
            headers: headers
        }).map(this.extractData).catch(this.handleError);
    }

    findEtatsLists(): Observable<Array<EnumType>> {
        return this.http.get(this.apiURL + '/etatsAnalyse').map(this.extractData).catch(this.handleError);
    }

    private extractData(res: Response) {
        let body = res.json();
        return body || {};
    }

    private handleError(error: Response | any) {
        // In a real world app, you might use a remote logging infrastructure
        let errMsg: string;
        if (error instanceof Response) {

            if (error.status === 401) {
                this.modalService.open(AuthModalContent);
            }
            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)

http状态可能是一个字符串,所以你应该使用一个简单的&#34; ==&#34;运算符或强制转换状态为int:

 if (parseInt(error.status, 10) === 401)