Angular JS' route'与组件%2F不匹配(已编码'/') [英] Angular JS 'route' doesn't match component with %2F (encoded '/')
问题描述
我在Angular JS中有一个路线",如下所示:
I have a 'route' in Angular JS as follows
$routeProvider.when('/foos/:fooId', { controller: FooController, templateUrl: 'foo.html'});
它非常有用,除非:fooId组件包含'/'或'%2F'(编码形式)
and it works great, unless the :fooId component contains either a '/' or '%2F' (encoded form)
我如何使这项工作有效,我的'fooId'可以包含/s?
How can I make this work, my 'fooId' can contain /s ?
推荐答案
您不容易做到这一点,因为如果您使用链接中带有%2F
的链接,浏览器将为您解码并结束/
. AngularJS当前不允许您在$route
参数中使用/
.
You can't easily do this because if you use a link with %2F
in it, the browser will just decode it for you and it'll end up being /
. AngularJS currently doesn't allow you to use /
in $route
params.
您可以对其进行双重编码,例如在以下plnkr中: http://plnkr.co/编辑/e04UMNQWkLRtoVOfD9b9?p =预览
You can double encode it, like in this plnkr: http://plnkr.co/edit/e04UMNQWkLRtoVOfD9b9?p=preview
var app = angular.module('app', []);
app.controller('HomeCtrl', function ($scope, $route) {
});
app.controller('DirCtrl', function ($scope, $route) {
var p = $route.current.params;
$scope.path = decodeURIComponent(p.p1);
});
app.config(function ($routeProvider) {
$routeProvider
.when('/', {templateUrl: 'home.html', controller: 'HomeCtrl'})
.when('/dir/:p1', {templateUrl: 'dir.html', controller: 'DirCtrl'})
.otherwise({redirectTo: '/'});
});
链接将为:<a href="#/dir/a%252Fb%252Fc">click here</a>
.
Another option, if you have a set number of /
characters in your parameters can be found here: How can I make the angular.js route the long path
这篇关于Angular JS&#39; route&#39;与组件%2F不匹配(已编码&#39;/&#39;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!