包含"点"的url查询参数的Angular ui-router

klo*_*ode 7 urlencode query-parameters angularjs angular-ui-router

在我们的Angular应用程序中,我们必须处理包含"点"的id.例如:

book = {
  id: '123.456'
}
Run Code Online (Sandbox Code Playgroud)

我们在使用这样的id作为url参数时遇到了问题.如果通过"Angular"进行导航,即点击调用的链接,一切运行良好$state.go('bookDetails', {bookId: book.id});.但重新加载页面时,事情不起作用

"无法获取/bookDetails?bookId=123.456"

在控制器中:

$scope.viewBookDetails = function() {
    $state.go('bookDetails', {bookId: book.id});
}
Run Code Online (Sandbox Code Playgroud)

在视图中

<a href="" ng-click="viewBookDetails(); $event.stopPropagation();">
Run Code Online (Sandbox Code Playgroud)

在路由器中:

.state('bookDetails', {
    url: '/bookDetails?bookId'
}
Run Code Online (Sandbox Code Playgroud)

在浏览器中:

https://example.com/bookDetails?bookId=123.456
Run Code Online (Sandbox Code Playgroud)

如果%2E在浏览器中替换"点",则链接有效.

我们试图在$ state.go()的参数中用"%2E"替换"dot"

$scope.viewBookDetails = function() {
    $state.go('bookDetails', {bookId: book.id.split('.').join('%2E')});
}
Run Code Online (Sandbox Code Playgroud)

但不起作用,因为"%"自动编码,浏览器中的"点"替换为"%252E"

https://example.com/bookDetails?bookId=123%252E456
Run Code Online (Sandbox Code Playgroud)

klo*_*ode 5

我使用包含'dot'的url查询参数获得的刷新问题是服务器问题.
它是由我在grunt服务器设置中处理html5mode(重定向到index.html,如果不是静态资源)的方式引起的

// The original grunt server settings
connect: {
  options: {
    port: 9000,
    // Change this to '0.0.0.0' to access the server from outside.
    hostname: 'localhost',
    livereload: 35729
  },
  livereload: {
    options: {
      open: true,
      middleware: function (connect) {
        return [
          require('connect-modrewrite')(['^[^\\.]*$ /index.html [L]']), //Matches everything that does not contain a '.' (period) and causes the problem
          connect.static('.tmp'),
          connect().use(
            '/bower_components',
            connect.static('./bower_components')
          ),
          connect().use(
            '/app/styles',
            connect.static('./app/styles')
          ),
          connect.static(appConfig.app)
        ];
      }
    }
  },
Run Code Online (Sandbox Code Playgroud)

我变了

require('connect-modrewrite')(['^[^\\.]*$ /index.html [L]']),
Run Code Online (Sandbox Code Playgroud)

require('connect-modrewrite')([
  '!\\.html|\\.js|\\.css|\\.svg|\\.jp(e?)g|\\.png|\\.gif|\\.ttf$ /index.html'
]),
Run Code Online (Sandbox Code Playgroud)