Mar*_*kus 9 angularjs angular-ui-router ui-sref
我正在寻找替换ui-sref中的字符的可能性,尊重目标的URL.
.state('base.product.detail', {
url: 'detail/:productName-:productId/'
Run Code Online (Sandbox Code Playgroud)
URL现在看起来像:
Now:
http://localhost/detail/My%20Product%20Name-123456789/
Should:
http://localhost/detail/My-Product-Name-123456789/
Run Code Online (Sandbox Code Playgroud)
我想摆脱%20(它们也直接在ui-sref =""中生成)并用减号( - )替换它们.
任何想法如何做到这一点?
此致,马库斯
Chr*_*s T 16
注册一个自定义类型,用于对数据进行编组和解组.文档:http://angular-ui.github.io/ui-router/site/#/api/ui.router.util.$ urlMatcherFactory
我们来定义一个自定义类型.实现编码,解码,是和模式:
var productType = {
encode: function(str) { return str && str.replace(/ /g, "-"); },
decode: function(str) { return str && str.replace(/-/g, " "); },
is: angular.isString,
pattern: /[^/]+/
};
Run Code Online (Sandbox Code Playgroud)
现在将自定义类型注册为"产品" $urlMatcherFactoryProvider:
app.config(function($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider) {
$urlMatcherFactoryProvider.type('product', productType);
}
Run Code Online (Sandbox Code Playgroud)
现在将url参数定义为产品,自定义类型将为您执行映射:
$stateProvider.state('baseproductdetail', {
url: '/detail/{productName:product}-:productId/',
controller: function($scope, $stateParams) {
$scope.product = $stateParams.productName;
$scope.productId = $stateParams.productId;
},
template: "<h3>name: {{product}}</h3><h3>name: {{productId}}</h3>"
});
Run Code Online (Sandbox Code Playgroud)
工作插件:http://plnkr.co/edit/wsiu7cx5rfZLawzyjHtf?p = preview