【问题标题】:Angular - Mongoose : Can't manipulate datesAngular - Mongoose:无法操纵日期
【发布时间】:2017-02-19 14:07:49
【问题描述】:

我一直在寻找解决方案,但没有任何帮助。

我有一个使用 Mongoose 和 Express 运行的 Angular JS 应用程序。

我想将日期存储为简单表单中的对象。 但是当我提交表单时,日期存储为字符串而不是对象。 所以我不能做这样的事情:

tache.start.getDate();

这是我的表格:

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

这是我的 Mongoose 架构:

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

这是我的控制器:

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

这是我得到的:

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

那么为什么我的日期会得到一个像 "2017-02-24T23:00:00.000Z" 这样的字符串而不是一个对象,而我的 Mongoose 架构却指定了 start: {type: Date, default: new Date()} 呢?

感谢您的帮助。

编辑

感谢 Saurabh Agrawal,我尝试在控制器中提交时转换日期:

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

遗憾的是,这并没有改变 :(

我还有一个日期作为字符串

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

编辑

我也尝试添加指令

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

并以我的形式调用它

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

但没有任何改变。

还有其他想法吗?

【问题讨论】:

  • 现在使用new Date()更改它
  • 感谢您的回答。我尝试了在我的版本帖子中显示的喜欢。但它不起作用。我错过了什么?

标签: javascript angularjs mongodb date object


【解决方案1】:

通过搜索,我发现我错过了真正的问题。

我的日期是日期对象。

当我这样做时

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

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

我明白了

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

所以像“2017-02-14T23:00:00.000Z”这样的日期显示是对象。

但在我的情况下,当我尝试对另一个对象中的日期执行相同操作时,例如在此架构中:

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

我什么都没得到:(

这段代码:

{{tache.start}}

这样显示日期“2017-02-20T23:00:00.000Z”

但是

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

什么都不显示。

我错过了什么?

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 2021-03-24
    • 2016-06-13
    • 2020-12-23
    • 1970-01-01
    • 2017-05-16
    • 2011-09-30
    • 2015-11-25
    • 1970-01-01
    相关资源
    最近更新 更多