限制与 AngularJS 状态匹配的动态 URL 参数的最佳方法
Best way to restrict dynamic URL parameters matched to an AngularJS state
目前我有一个状态 url 定义为:
…/:table/edit/:id
-> 因此 urls:
…/products/edit/5 & …/notValidTable/edit/5
加载我的table编辑状态,定义如下:
tableEdit.state.js:
angular.module(‘myApp')
.config(function($stateProvider) {
$stateProvider
.state('tableEdit', {
url: '/:table/edit/:id',
templateUrl: ‘../table_edit/table_edit.html',
controller: 'TableEditCtrl'
});
});
Q - 据我所知,我在 tableEdit 中的“:table”参数只能是以下之一:“产品”、“供应商”、“客户”、“关键字”或“ topics',如何限制我的状态接受的加载值?
这是我的解决方案,但我希望得到反馈或解决此问题的更好方法:
app.js:
angular.module(‘myApp', [
'ui.router',
])
.config(
['$urlRouterProvider',
function($urlRouterProvider) {
$urlRouterProvider
.when('/:table/edit/:id', ['$match', '$state',
function($match, $state) {
if ($match.table !== 'products'
&& $match.table !== 'providers'
&& $match.table !== 'customer'
&& $match.table !== 'keywords'
&& $match.table !== 'topics') {
$state.transitionTo('tablesHome');
} else {
$state.transitionTo('tableEdit', $match, true); }
}
}
])
.otherwise('/home');
])
是否有更好的方法来限制 tableEdit 的“:table”参数的可能值,以便我的页面针对无效参数值进行适当的重定向?
非常感谢。
您可以在路由参数上放置一个正则表达式。这是一个工作示例:
var app = angular.module('myApp', [
'ui.router',
])
app.config(['$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
$stateProvider
.state('table', {
// this is the important bit.
// it will restrict this route to only 'products' and 'customers
url: '/{table:products|customers}'
})
.state('table.edit', {
url: '/edit/:id',
template: "<h1>TABLE</h1>"
})
.state('home', {
url: '/*path', // special 'catch all route',
template: "<h1>HOME</h1>"
});
$urlRouterProvider.otherwise('/home');
}]);
app.run(['$rootScope', '$state', function($rootScope, $state) {
$rootScope.$state = $state;
}])
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.4.0-beta.5" data-semver="1.4.0-beta.5" src="https://code.angularjs.org/1.4.0-beta.5/angular.js"></script>
<script data-require="ui-router@0.2.13" data-semver="0.2.13" src="//rawgit.com/angular-ui/ui-router/0.2.13/release/angular-ui-router.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="myApp">
<h1>Hello Plunker!</h1>
<a href="#/products/edit/5">/products/edit/5</a><br/>
<a href="#/customers/edit/5">/customers/edit/5</a><br/>
<a href="#/not-a-table/edit/5">/not-a-table/edit/5</a><br/>
<a href="#/this-is-a-random-url">/this-is-a-random-url</a><br/>
<div ui-view=""></div>
<pre>
<!-- Here's some values to keep an eye on in the sample in order to understand $state and $stateParams -->
$state = {{$state.current.name}}
$params = {{$state.params}}
$state full url = {{ $state.$current.url.source }}
</pre>
</body>
</html>
https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters
目前我有一个状态 url 定义为:
…/:table/edit/:id
-> 因此 urls:
…/products/edit/5 & …/notValidTable/edit/5
加载我的table编辑状态,定义如下:
tableEdit.state.js:
angular.module(‘myApp')
.config(function($stateProvider) {
$stateProvider
.state('tableEdit', {
url: '/:table/edit/:id',
templateUrl: ‘../table_edit/table_edit.html',
controller: 'TableEditCtrl'
});
});
Q - 据我所知,我在 tableEdit 中的“:table”参数只能是以下之一:“产品”、“供应商”、“客户”、“关键字”或“ topics',如何限制我的状态接受的加载值?
这是我的解决方案,但我希望得到反馈或解决此问题的更好方法:
app.js:
angular.module(‘myApp', [
'ui.router',
])
.config(
['$urlRouterProvider',
function($urlRouterProvider) {
$urlRouterProvider
.when('/:table/edit/:id', ['$match', '$state',
function($match, $state) {
if ($match.table !== 'products'
&& $match.table !== 'providers'
&& $match.table !== 'customer'
&& $match.table !== 'keywords'
&& $match.table !== 'topics') {
$state.transitionTo('tablesHome');
} else {
$state.transitionTo('tableEdit', $match, true); }
}
}
])
.otherwise('/home');
])
是否有更好的方法来限制 tableEdit 的“:table”参数的可能值,以便我的页面针对无效参数值进行适当的重定向?
非常感谢。
您可以在路由参数上放置一个正则表达式。这是一个工作示例:
var app = angular.module('myApp', [
'ui.router',
])
app.config(['$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
$stateProvider
.state('table', {
// this is the important bit.
// it will restrict this route to only 'products' and 'customers
url: '/{table:products|customers}'
})
.state('table.edit', {
url: '/edit/:id',
template: "<h1>TABLE</h1>"
})
.state('home', {
url: '/*path', // special 'catch all route',
template: "<h1>HOME</h1>"
});
$urlRouterProvider.otherwise('/home');
}]);
app.run(['$rootScope', '$state', function($rootScope, $state) {
$rootScope.$state = $state;
}])
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.4.0-beta.5" data-semver="1.4.0-beta.5" src="https://code.angularjs.org/1.4.0-beta.5/angular.js"></script>
<script data-require="ui-router@0.2.13" data-semver="0.2.13" src="//rawgit.com/angular-ui/ui-router/0.2.13/release/angular-ui-router.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="myApp">
<h1>Hello Plunker!</h1>
<a href="#/products/edit/5">/products/edit/5</a><br/>
<a href="#/customers/edit/5">/customers/edit/5</a><br/>
<a href="#/not-a-table/edit/5">/not-a-table/edit/5</a><br/>
<a href="#/this-is-a-random-url">/this-is-a-random-url</a><br/>
<div ui-view=""></div>
<pre>
<!-- Here's some values to keep an eye on in the sample in order to understand $state and $stateParams -->
$state = {{$state.current.name}}
$params = {{$state.params}}
$state full url = {{ $state.$current.url.source }}
</pre>
</body>
</html>
https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters