$stateProvider
.state('messagelogs', {
controller: 'Messages',
url:'/messageLogs/{pageIndex:int}/{itemsPerPage:int}/{text:string}/{fromDate:date}
})
ui-router会识别:date吗?
您可以创建自己的类型。因此,当您说$ 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句