简体   繁体   English

预期为日期angular.js

[英]Expected to be a date angular.js

I'm pulling this JSON from the mongodb, and I have a date field which have the type as DATE. 我从mongodb中提取此JSON,并且有一个日期字段,其类型为DATE。

I want be able to change the date if needed in a interface, for this I need a input type date field with the attribute ng-model="event.date", just like this: 我希望能够根据需要在界面中更改日期,为此,我需要具有ng-model =“ event.date”属性的输入类型日期字段,如下所示:

<input class="tables__sidebar-input" type="date" ng-model='event.date'>

This is throwing an error: 这引发了一个错误:

Error: [ngModel:datefmt] Expected `2015-09-03T00:00:00.000Z` to be a date

I found the reason for the error and fixed it. 我找到了错误的原因并进行了修复。 I found this directive which SHOULD fix the error: 我发现该指令应该修复错误:

angular.module('adminApp')
  .directive("myModelFilter", ["$filter", function($filter) {
    return {
      restrict: 'A', // only activate on element attribute
      require: '?ngModel',
      link: function(scope, element, attrs, ngModelController) {
        ngModelController.$parsers.push(function(data) {
          console.log(data);
          //convert data from view format to model format

          // Grab the parameters
          var params = scope.$eval(attrs.myModelFilter);

          // Filter with the parameters passed
          return $filter(params.filter)(data, params.expr, params.comp);

        });

        ngModelController.$formatters.push(function(data) {
          //convert data from model format to view format
          // Grab the parameters
          var params = scope.$eval(attrs.myModelFilter);

          // Filter with the parameters passed
          return $filter(params.filter)(data, params.expr, params.comp);
        });
      }
    };
  }])

To use, simply add my-model-filter="{filter:'date', expr:'yyyy-MM-dd'}" in the input field, which I did: 要使用,只需在输入字段中添加my-model-filter="{filter:'date', expr:'yyyy-MM-dd'}" ,即可:

<input class="tables__sidebar-input" type="date" ng-model='event.date' my-model-filter="{filter:'date', expr:'yyyy-MM-dd'}">

But now is throwing the same error with the valid format: 但是现在使用有效格式抛出了相同的错误:

Error: [ngModel:datefmt] Expected `2015-09-02` to be a date

What's missing here? 这里缺少什么?

I'm pulling the data from a model service by a controller: 我正在通过控制器从模型服务中提取数据:

retrieve action: 检索动作:

retrieve: function() {
    var that = this;

    this.EventModel.Model.find()
        .then(function(result) {
            that.$scope.events = result;
        });
}

Please guys, help me! 拜托,帮帮我! I already read every stackoverflow answer, every post blog and I cant figure out how to fix it. 我已经阅读了每个stackoverflow的答案,每个博文,而且我想不出如何解决它。

I don't want use a mask with the type text, I want use type="date" which is the right format. 我不想使用文本类型的蒙版,我想使用正确的格式type =“ date”。

This is what I use. 这就是我用的。 I delete all existings $formatters, $parsers with .splice, since I don't need that, but you may want to keep those. 我删除了所有现有的$ formatters和$ parsers和.splice,因为我不需要它,但是您可能想要保留这些。

How to use on directive is like type="date" as-date 如何在指令上使用就像type="date" as-date

angular.module('someApp')
    .directive('asDate', function () {
    return {
        require: '^ngModel',
        restrict: 'A',
        link: function (scope, element, attrs, ctrl) {
            ctrl.$formatters.splice(0, ctrl.$formatters.length);
            ctrl.$parsers.splice(0, ctrl.$parsers.length);
            ctrl.$formatters.push(function (modelValue) {
                if (!modelValue) {
                    return;
                }
                return new Date(modelValue);
            });
            ctrl.$parsers.push(function (modelValue) {
                return modelValue;
            });
        }
    };
});

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

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