$stateProvider
.state('messagelogs', {
controller: 'Messages',
url:'/messageLogs/{pageIndex:int}/{itemsPerPage:int}/{text:string}/{fromDate:date}
})
ui-router会识别:date?
答案 0 :(得分:1)
您可以创建自己的类型。因此,当您说$ stateParams.someDate属性时,您将始终获得Date的实例。 这是一些例子,你可以改进它
(function (angular, Date) {
angular.module('some-module')
.config(addCustomUrlParamType);
addCustomUrlParamType.$inject = ['$urlMatcherFactoryProvider'];
function addCustomUrlParamType($urlMatcherFactoryProvider) {
$urlMatcherFactoryProvider.type('myDate', {
name: 'myDate',
decode: function (myDateValue) {
var timestamp;
if(angular.isDate(myDateValue)) {
return myDateValue;
}
timestamp = Date.parse(myDateValue);
if(!isNan(timestamp)) {
return new Date(timestamp);
}
},
/**
* @description
* Detects whether a value is of a particular type. Accepts a native (decoded) value and determines whether
* it matches the current Type object.
*/
is: function (myDateValue) {
return angular.isDefined(myDateValue) && this.decode(myDateValue) === myDateValue;
}
})
}
}(this.angular, this.Date));
所以,现在你可以这样说:
$stateProvider
.state('messagelogs', {
controller: 'Messages',
url:'/messageLogs/{pageIndex:int}/{itemsPerPage:int}/{text:string}/{fromDate:myDate}
});
如果您想通过日期,可以说:
$state.go('messagelogs', {fromDate: (new Date()).toISOString()})
或者如果你提供了从日期创建字符串的编码函数:
$state.go('messagelogs', {fromDate: new Date()})
您可以找到更多信息: http://angular-ui.github.io/ui-router/site/#/api/ui.router.util.type:Type