角网址加号转换为空格
收藏

我有一个角度应用程序,我想在查询字符串中传递加号+:

http://localhost:3000/page?name=xyz+manwal

当我点击此URL时,其转换为:

http://localhost:3000/page?name=xyz%20manwal

Where %20 refer to space . How can I prevent this conversion?

最佳答案

I have found solution and posting it for future reference. Angular js was converting + sign into %2B.

以下代码阻止了此操作:

.config([
    '$provide', function($provide) {
      $provide.decorator('$browser', function($delegate) {
        let superUrl = $delegate.url;
        $delegate.url = (url, replace) => {
          if(url !== undefined) {
            return superUrl(url.replace(/\%2B/g,"+"), replace);
          } else {
            return superUrl().replace(/\+/g,"%2B");
          }
        };
        return $delegate;
      });
    }
  ])

    公众号
    关注公众号订阅更多技术干货!