2016-01-12 106 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