错误:[ngModel:datefmt] 预期 `2015-05-29T19:06:16.693209Z` 是一个日期 - Angular [英] Error: [ngModel:datefmt] Expected `2015-05-29T19:06:16.693209Z` to be a date - Angular

查看:18
本文介绍了错误:[ngModel:datefmt] 预期 `2015-05-29T19:06:16.693209Z` 是一个日期 - Angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Djangorest-framework 开发 angular 应用程序..

I'm working on angularapplication with Django with rest-framework..

应用程序从服务器接收带有 json 的信息.其中一个键是 created_time... 该字段的值是根据 iso-8601 的格式,例如 2015-05-29T19:06:16.693209Z.

The app receives it's info with json from the server.. One of the keys is created_time... The value of this field is format according to iso-8601, for example 2015-05-29T19:06:16.693209Z.

在客户端我有一个字段:

In the client I have a field:

<input type="time" ng-model="created_time">

但是当数据到达时,我收到此错误:

But when the data is arriving I get this error:

Error: [ngModel:datefmt] Expected `2015-05-29T19:06:16.693209Z` to be a date http://errors.angularjs.org/1.3.13/ngModel/datefmt?p0=2015-05-29T19%3A06%3A16.693209Z
at REGEX_STRING_REGEXP (angular.js:63)
at Array.<anonymous> (angular.js:19807)
at Object.ngModelWatch (angular.js:23289)
at Scope.$get.Scope.$digest (angular.js:14235)
at Scope.$get.Scope.$apply (angular.js:14506)
at done (angular.js:9659)
at completeRequest (angular.js:9849)
at XMLHttpRequest.requestLoaded (angular.js:9790)

我已经尝试了一切:(格式与angular文档中的说明完全相同...

I already tried everything :( the format is exactly as the instructions in the docs of angular...

推荐答案

这一定发生在 angular 1.3+ 中.1.3+ 用于日期/时间输入的病房 ng-model 需要是有效的日期对象,不再允许日期的字符串表示.您需要将字符串转换为日期对象 ($scope.created_time = new Date(dateString)) 并将其绑定到 ng-model.如果您按照错误链接它清楚地描述了错误以及如何解决它.

This must be happening with angular 1.3+. 1.3+ on wards ng-model for date/time input needs to be a valid date object, string representation of date is no longer allowed. You need to convert string to date object ($scope.created_time = new Date(dateString)) and bind it to the ng-model. If you follow the error link it has a clear description about the error and how to resolve it.

所有与日期相关的输入都要求模型是一个日期对象.如果模型是别的东西,就会抛出这个错误.在这种情况下,Angular 不会设置验证错误,因为这些错误会显示给用户,但错误状态是由不正确的应用程序逻辑引起的,而不是由用户引起的.

All date-related inputs like require the model to be a Date object. If the model is something else, this error will be thrown. Angular does not set validation errors on the in this case as those errors are shown to the user, but the erroneous state was caused by incorrect application logic and not by the user.

这篇关于错误:[ngModel:datefmt] 预期 `2015-05-29T19:06:16.693209Z` 是一个日期 - Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆