【发布时间】:2017-02-19 14:07:49
【问题描述】:
我一直在寻找解决方案,但没有任何帮助。
我有一个使用 Mongoose 和 Express 运行的 Angular JS 应用程序。
我想将日期存储为简单表单中的对象。 但是当我提交表单时,日期存储为字符串而不是对象。 所以我不能做这样的事情:
tache.start.getDate();
这是我的表格:
<form name="formTache" ng-submit="addTache(tache)">
<div class="form-group row">
<div class="col-lg-12">
<input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date"/>
</div>
</div>
<div class="form-group row">
<div class="text-right col-lg-12">
<button type="submit" class="btn btn-default">Ajouter</button>
</div>
</div>
这是我的 Mongoose 架构:
var restful = require('node-restful');
var mongoose = restful.mongoose;
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var tachesSchema = new mongoose.Schema({
title : String,
start: {type: Date, default: new Date()},
});
var tachesModel = mongoose.model('taches', tachesSchema);
module.exports = restful.model('taches', tachesSchema);
这是我的控制器:
angular.module('personaldashboard.tache', [])
.controller('TachesCtrl', function($scope, Taches, Progress, toaster) {
$scope.tache = new Taches();
var refreshTache = function() {
$scope.taches = Taches.query();
$scope.tache = ''
}
refreshTache();
$scope.addTache = function(tache) {
Taches.save(tache,function(tache){
refreshTache();
});
};
$scope.updateTache = function(tache) {
tache.$update(function(){
refreshTache();
});
};
$scope.removeTache = function(tache) {
tache.$delete(function(){
refreshTache();
});
};
$scope.editTache = function(id) {
$scope.tache = Taches.get({ id: id });
};
$scope.deselectTache = function() {
$scope.tache = ''
}
$scope.editTache_Projet = function(tache, projetId) {
tache.projet = projetId;
tache.$update(function(){
refreshTache();
});
};
});
这是我得到的:
{ "_id": "58a99df24975ad0104c692b1", "title": "Test", "start": "2017-02-24T23:00:00.000Z" }
那么为什么我的日期会得到一个像 "2017-02-24T23:00:00.000Z" 这样的字符串而不是一个对象,而我的 Mongoose 架构却指定了 start: {type: Date, default: new Date()} 呢?
感谢您的帮助。
编辑
感谢 Saurabh Agrawal,我尝试在控制器中提交时转换日期:
$scope.addTache = function(tache) {
tache.start = new Date(tache.start);
tache.end = new Date(tache.end);
Taches.save(tache,function(tache){
refreshTache();
});
};
遗憾的是,这并没有改变 :(
我还有一个日期作为字符串
"2017-02-20T23:00:00.000Z"
编辑
我也尝试添加指令
.directive("formatDate", function(){
return {
scope: {ngModel:'='},
link: function(scope) {
if (scope.ngModel) {
scope.ngModel = new Date(scope.ngModel);
}
}
}
})
并以我的形式调用它
<form name="formTache" ng-submit="addTache(tache)">
<div class="form-group row">
<div class="col-lg-12">
<input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date" formatDate/>
</div>
</div>
<div class="form-group row">
<div class="text-right col-lg-12">
<button type="submit" class="btn btn-default">Ajouter</button>
</div>
</div>
但没有任何改变。
还有其他想法吗?
【问题讨论】:
-
现在使用
new Date()更改它 -
感谢您的回答。我尝试了在我的版本帖子中显示的喜欢。但它不起作用。我错过了什么?
标签: javascript angularjs mongodb date object