如何通过Angular传递URL?

时间:2018-10-15 11:05:05

标签: angular url

这是我要尝试的操作,但我知道URL中的斜杠不正确

http://localhost:57101/api/employee/holiday?userName=dinchmle&Date=05/10/2018&StateVal=2

这是我的角度服务代码

 updateEmployee(userName: string, holiday: Holiday): Observable<Employee> {
    return this.http.put(this._employeeUrl + '/holiday?userName=' + userName + '&Date=' + holiday.date + '&StateVal=' + holiday.state, '')
    .map((res: Employee) => res)
    .catch(this.handleError);
  }

2 个答案:

答案 0 :(得分:1)

只需使用encodeURI函数对您的网址进行编码。这将包含您网址中的所有特殊字符。

 updateEmployee(userName: string, holiday: Holiday): Observable<Employee> {
    let url = this._employeeUrl + '/holiday?userName=' + userName + '&Date=' + holiday.date + '&StateVal=' + holiday.state;

    return this.http.put(encodeURI(url), '') // encode URL
    .map((res: Employee) => res)
    .catch(this.handleError);
  }

答案 1 :(得分:1)

您可以将斜杠转为%2F

这是编辑后的示例:

updateEmployee(userName: string, holiday: Holiday): Observable<Employee> {
    let parsedDate = Date.parse(holiday.date);
    let month = parsedDate.getUTCMonth() + 1; //months from 1-12
    let day = parsedDate.getUTCDate();
    let year = parsedDate.getUTCFullYear();

    return this.http.put(this._employeeUrl + '/holiday?userName=' + userName + '&Date=' + day + '%2F' + month + '%2F' + year + '&StateVal=' + holiday.state, '')
        .map((res: Employee) => res)
        .catch(this.handleError);
}