简体   繁体   English

Angular-猫鼬:无法操纵日期

[英]Angular - Mongoose : Can't manipulate dates

I have been searching for a long while for solution, but nothing helped me. 我一直在寻找解决方案很长时间,但没有任何帮助。

I have an Angular JS app which runs with Mongoose and Express. 我有一个与Mongoose和Express一起运行的Angular JS应用程序。

I want to store date as object from a simple form. 我想通过简单的形式将日期存储为对象。 But when I submit my form, dates are stored as String and not as Objects. 但是,当我提交表单时,日期存储为字符串而不是对象。 So I can't do something like : 所以我不能做这样的事情:

tache.start.getDate();

Here is my form : 这是我的表格:

<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>

Here is my Mongoose Schema : 这是我的猫鼬模式:

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);

Here is my Controller : 这是我的控制器:

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();
      });
    };
    });

Here is what I get : 这是我得到的:

{ "_id": "58a99df24975ad0104c692b1", "title": "Test", "start": "2017-02-24T23:00:00.000Z" }

So why do I get a string like "2017-02-24T23:00:00.000Z" for my date instead of an object whereas my Mongoose schema specify start: {type: Date, default: new Date()} ? 那么,为什么我的日期而不是对象的字符串为"2017-02-24T23:00:00.000Z" ,而我的Mongoose模式指定了start: {type: Date, default: new Date()}呢?

Thanks for your help. 谢谢你的帮助。

EDIT 编辑

Thanks to Saurabh Agrawal, I tried to convert the date when submiting in the controller : 感谢Saurabh Agrawal,我尝试在控制器中提交时转换日期:

$scope.addTache = function(tache) {
  tache.start = new Date(tache.start);
  tache.end = new Date(tache.end);
  Taches.save(tache,function(tache){
    refreshTache();
  });
};

Sadly, this changed nothing :( 可悲的是,这并没有改变:(

I still have a date as string 我还有一个日期作为字符串

 "2017-02-20T23:00:00.000Z" 

EDIT 编辑

I also tried to add a directive 我也尝试添加指令

.directive("formatDate", function(){
  return {
   scope: {ngModel:'='},
    link: function(scope) {
        if (scope.ngModel) {
            scope.ngModel = new Date(scope.ngModel);
        }
    }
  }
})

and call it in my form 并以我的形式称呼它

<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>

But nothing changes. 但是什么都没有改变。

Any other ideas ? 还有其他想法吗?

By searching, I 've found that I was missing the real problem. 通过搜索,我发现我没有找到真正的问题。

My dates are date objects. 我的日期是日期对象。

When i do this 当我这样做

$scope.test = new Date([2017,2,15]);

<pre>{{test}}</pre>
<pre>{{test.getDate()}}</pre>

I get 我懂了

"2017-02-14T23:00:00.000Z" and 15 “ 2017-02-14T23:00:00.000Z”和15

So date displaying like "2017-02-14T23:00:00.000Z" are objects. 因此,日期显示为“ 2017-02-14T23:00:00.000Z”是对象。

But in my case, when i try to do the same with a date whitch is in another object like in this schema : 但是在我的情况下,当我尝试对日期调整执行相同操作时,在另一个对象中,如该模式所示:

 var tachesSchema = new mongoose.Schema({
    title : String,
    start: {type: Date, default: new Date()},
    end: {type: Date, default: new Date()},
    comment : String,
    state : Boolean,
    projet : { type: ObjectId, ref: 'Projets' }
});

I get nothing :( 我什么也没得到:(

This code : 此代码:

{{tache.start}}

displays the date like this "2017-02-20T23:00:00.000Z" 显示这样的日期“ 2017-02-20T23:00:00.000Z”

but

<pre>{{tache.start.getDate()}}</pre>

displays nothing. 什么都不显示。

What I missed ? 我错过了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM