用angularJS更新mongodb中的date,inputtypes=“date”
我有一个MongoDb 学生collections。 一个学生可以有一些包含开始和结束的课程(opl)。 我使用Mongoose在server.js中创build一个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和HTML5inputtypes=“date”这需要一个Date()对象来显示startdatum和einddatumdate? 我使用这样的Angular服务来创build$ 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)来创build一个Date对象来使用
<input type="date" class="form-control" ng-model="opl.startdatum " />
这样它的值是一个ISOstring,我可以用它来更新课程date。 我一直在读自己愚蠢的,但没有任何人显示如何将Mongoose ISOString转换为Date对象…所有的例子开始与他们自己创build一个新的Date()对象。 $ scope.student.opl不允许我将date更改为date对象。 我似乎没有能够添加一个字段。 filter不起作用。 其实这下面显示的date是正确的,但它会抛出一个错误,更新值是空的:
<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对象。
我可能在这里错过了一个关键的战略点,但我会感谢一些帮助,告诉我我的错误,谢谢,
一月
您可以通过以下格式将ISOstring转换为date对象,
var date = new Date('2015-11-13T06:16:11.399Z')
在你的情况下,它会像,
$ scope.student.opl.startdatum = new Date($ scope.student.opl.startdatum);