【发布时间】:2016-01-12 10:01:24
【问题描述】:
我有一个 MongoDb student 集合。一个学生可以有许多课程(opl),其中包含开始日期和结束日期。 我使用 Mongoose 在 server.js 中创建 REST 接口:
//adres Schema
var adresSchema = new mongoose.Schema({
gemeente: String,
post: Number,
straat: String
})
//opl Schema
var oplSchema = new mongoose.Schema({
oplcode: Number,
geslaagd: Boolean,
startdatum: Date,
einddatum: Date,
})
//Studenten Schema
var studentSchema = new mongoose.Schema({
voornaam: String,
familienaam: String,
email: String,
adres: adresSchema,
foto: String,
ikl: Number,
opl: [oplSchema],
rijksreg: String,
gender: String,
tel: [String]
}, {collection:"studenten"})
要查询我在模型上使用这条路线的学生:
/GET /api/studenten/:id 1 enkele student by id
apiRouter.get('/studenten/:id', function(req, res) {
return studentModel.findById(req.params.id,function(err, student) {
if (!err) {
res.send(student);// 1 student
}
else {
return console.log(err);
}
})
})
我的问题是如何使用 Angular 和 HTML5 输入 type="date" 这需要 Date() 对象来显示 startdatum 和 einddatum 日期? 我使用这样的 Angular 服务来创建 $scope:
.factory('studentService',['$resource', function($resource){
var urlBase = "api/studenten";
var studentResource = $resource(
urlBase + '/:_id',
{_id: '@_id'},
{update: {method:'PUT'}
}
)
return studentResource;
}])
然后在学生编辑控制器中,我用服务填充 $scope:
.controller('StudentEditCtrl', ['$scope', '$routeParams', 'studentService', 'opleidingService','$location',
function ($scope, $routeParams, studentService, opleidingService, $location) {
//update student and his courses (opl)
$scope.subtitel = "Update student";
$scope.student = studentService.get({}, {'_id': $routeParams._id});
//save
$scope.save = function (student) {
if ($scope.student._id) {
//update
console.log("updating student " + student.familienaam);
studentService.update({_id: $scope.student._id}, $scope.student);
}
else {
//new student
$scope.student.$save();
}
$location.path('/studenten');
}
//更多方法,有些东西省略了
我如何使用我的 $scope.student.opl.startdatum dateStrings (ISODates) 创建一个 Date 对象以用于
<input type="date" class="form-control" ng-model="opl.startdatum " />
这样它的值就是一个 ISO 字符串,我可以用它来更新课程日期。 我一直在读傻,但没有人展示如何将 Mongoose ISOString 转换为 Date 对象......所有示例都以他们自己创建的新 Date() 对象开头。 $scope.student.opl 不允许我将日期更改为日期对象。我似乎无法向其中添加字段。过滤器不起作用。 实际上下面这个正确显示了日期,但是它抛出了很多错误并且更新值是空的:
<input type="text" class="form-control" ng-model="opl.einddatum" value="{{opl.einddatum | date:'yyyy-MM-dd'}}" />
使用类似的方法
<input type="date" class="form-control" ng-model="makeDate(opl.startdatum) " />
也不行。我查看了 angular-ui-bootstrap,但这里是一样的:你需要一个 Date 对象。
我可能在这里遗漏了一个关键的战略点,但我希望能得到一些帮助来告诉我我的错误,谢谢,
一月
【问题讨论】:
标签: javascript angularjs node.js mongodb date