2016-01-12 104 views
1

我有一个MongoDb 学生集合。一个学生可以有多个课程(opl),其中包含开始和结束。 我用猫鼬创造server.js REST接口:更新日期在mongodb与angularJS和输入类型=“日期”

//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和需要Date()对象的HTML5输入类型=“date”来显示startdatumeinddatum日期? 我用这样一个角度的服务创造了$范围:在学生编辑控制器我填的是$范围与服务

.factory('studentService',['$resource', function($resource){ 
     var urlBase = "api/studenten"; 
     var studentResource = $resource(
      urlBase + '/:_id', 
      {_id: '@_id'}, 
      {update: {method:'PUT'} 
      } 
     ) 
     return studentResource; 
    }]) 

然后:

.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对象。

我可能错过了重要的战略点这里,但我希望得到一些帮助,给我看我的错误,谢谢,

回答

2

您可以通过以下格式的ISO字符串转换为日期对象,

VAR日期=新的日期( '2015-11-13T06:16:11.399Z')

在你的情况下,它会像,

$ scope.student.opl.startdatum = new Date($ scope.student.opl。startdatum);

+0

嗨库马尔,这does'nt工作,因为我的服务返回$资源,这是异步的:我不能直接编辑$ scope.student。 所以我的问题归结为:我如何编辑/更改/添加到异步资源之前(或之后)我填充它的范围? – user3683637