WJM*_*WJM 15 date mongoose angularjs angular-material
我有一个mongoose连接到包含集合中的Date对象的数据库.我想使用Angular Material的DatePicker控件查看这些Date对象.Date对象遵循ISO字符串格式.
这是一段代码:
<md-datepicker
ng-model="license.expirationdate" md-placeholder="Enter date">
</md-datepicker>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
在ng-model为md-datepicker必须是一个日期实例.
在研究时,我发现你可以使用过滤器来创建一个Date实例,但这对我不起作用 - >我收到一条错误消息,说明在使用简单过滤器时模型值是不可分配的.过滤器只是根据字符串输入返回一个新的Date对象.
如何在仍然允许ng-model更改的情况下将字符串格式化为Date对象?
编辑:mongoose var的架构Schema = mongoose.Schema;
var Schema = mongoose.Schema;
var modelschema = new Schema({
name : String,
licensetype : String,
activationcount : Number,
expirationdate: Date,
key : String
})
Run Code Online (Sandbox Code Playgroud)
这是填充模式的快速路由
app.post('/licenses', function (req, res) {
console.log(req.body.expirationDate);
License.create({
name: req.body.licenseName,
licensetype: req.body.licenseType,
activationcount: 0,
expirationdate: req.body.expirationDate,
key: "123456"
}, function (err, license) {
if (err) {
res.send(err);
console.log(err);
}
//Send user back to main page
res.writeHead(301, {
'Location': '/',
'Content-Type': 'text/plain'
});
res.end();
}
)
});
Run Code Online (Sandbox Code Playgroud)
mas*_*asa 16
这是一个例子:
标记:
<div ng-controller="MyCtrl">
<md-datepicker ng-model="dt" md-placeholder="Enter date" ng-change="license.expirationdate = dt.toISOString()">
</md-datepicker>
{{license.expirationdate}}
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
app.controller('MyCtrl', function($scope) {
$scope.license = {
expirationdate: '2015-12-15T23:00:00.000Z'
};
$scope.dt = new Date($scope.license.expirationdate);
});
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/masa671/jm6y12un/
更新:
用ng-repeat:
标记:
<div ng-controller="MyCtrl">
<div ng-repeat="d in data">
<md-datepicker
ng-model="dataMod[$index].dt"
md-placeholder="Enter date"
ng-change="d.license.expirationdate = dataMod[$index].dt.toISOString()">
</md-datepicker>
{{d.license.expirationdate}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
app.controller('MyCtrl', function($scope) {
var i;
$scope.data = [
{ license:
{ expirationdate: '2015-12-15T23:00:00.000Z' }
},
{ license:
{ expirationdate: '2015-12-20T23:00:00.000Z' }
},
{ license:
{ expirationdate: '2015-12-25T23:00:00.000Z' }
}
];
$scope.dataMod = [];
for (i = 0; i < $scope.data.length; i += 1) {
$scope.dataMod.push({
dt: new Date($scope.data[i].license.expirationdate)
});
}
});
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/masa671/bmqpyu8g/
Jef*_*son 10
您可以使用ng-init,自定义过滤器和ng-change,并在标记中完成此操作.
JavaScript的:
app.filter('toDate', function() {
return function(input) {
return new Date(input);
}
})
Run Code Online (Sandbox Code Playgroud)
HTML:
<md-datepicker
ng-init="date = (license.expirationdate | toDate)"
ng-model="date"
ng-change="license.expirationdate = date.toISOString()"
md-placeholder="Enter date">
</md-datepicker>
Run Code Online (Sandbox Code Playgroud)
使用这种方法,您不需要使用View逻辑来混淆Controller代码.缺点是Controller中对license.expirationdate的任何编程更改都不会自动反映在View中.
http://jsfiddle.net/katfby9L/1/
// Configure the $httpProvider by adding our date transformer
app.config(["$httpProvider", function ($httpProvider) {
$httpProvider.defaults.transformResponse.push(function(responseData){
convertDateStringsToDates(responseData);
return responseData;
});
}]);
var regexIso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
function convertDateStringsToDates(input) {
// Ignore things that aren't objects.
if (typeof input !== "object") return input;
for (var key in input) {
if (!input.hasOwnProperty(key)) continue;
var value = input[key];
var match;
// Check for string properties which look like dates.
// TODO: Improve this regex to better match ISO 8601 date strings.
if (typeof value === "string" && (match = value.match(regexIso8601))) {
// Assume that Date.parse can parse ISO 8601 strings, or has been shimmed in older browsers to do so.
var milliseconds = Date.parse(match[0]);
if (!isNaN(milliseconds)) {
input[key] = new Date(milliseconds);
}
} else if (typeof value === "object") {
// Recurse into object
convertDateStringsToDates(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将自动将服务器JSON响应中的所有字符串转换为日期
我会这样做:
HTML:
<div ng-controller="MyCtrl">
<div ng-repeat="d in data">
<md-datepicker
ng-init="date = StrToDate(d.license.expirationdate);"
ng-model="date"
md-placeholder="Enter date"
ng-change="d.license.expirationdate = date.toISOString()">
</md-datepicker>
{{d.license.expirationdate}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在你的控制器中
$scope.StrToDate = function (str) {
return new Date(str);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32293 次 |
| 最近记录: |