AngularJS - 初始化DATE输入 [英] AngularJS - Initializing DATE input
问题描述
我有一个应用程序,其中有几个日期字段需要接受来自用户和数据库的值。
I have an application in which there are several date fields that need to accept values from the user as well as from the database.
我发现这个解决方案,但仍然收到错误消息
I found this solution but still get the error message
错误:[ngModel:datefmt]
Error: [ngModel:datefmt]
http ://errors.angularjs.org/1.5.8/ngModel/datefmt?p0 = 2017-05-03
我的代码是(你可以看到我在评论中做了多次尝试):
My code is (you can see a number of attempts I made in comments as well):
var xx = $filter("date")(Date.now(), 'yyyy-MM-dd');
var yy = $filter("date")(Date.now(), 'yyyy-MM-dd');
$rootScope.Global.Time_Window_From = xx; // $filter("date")(Date.now(), 'yyyy-MM-dd'); //'2017-05-02' ; //Date.now() ; //$filter("date")(Date.now(), 'yyyy-MM-dd') ;
$rootScope.Global.Time_Window_To = yy; // $filter("date")(Date.now(), 'yyyy-MM-dd'); //'2017-05-02' ; //Date.now() ; //$filter("date")(Date.now(), 'yyyy-MM-dd') ;
此外,与 DATE 数据交换的最佳方式是什么?一个SQL-Server数据库?
In addition, what would be the best way to exchange DATE data with an SQL-Server database?
提前致谢。
推荐答案
来自Angularjs的文档:
Documentation from Angularjs:
所有与日期相关的输入都要求模型为日期对象。如果模型是别的,那么将抛出此错误
All date-related inputs like require the model to be a Date object. If the model is something else, this error will be thrown
你应该绑定到日期类型
输入只有日期类型
数据。
按新日期(参数)
将生成数据的日期对象。
You should bind to date-type
input with only date-type
data.
By new Date(parameter)
will generate a date object of your data.
angular.module("app", [])
.controller("myCtrl", function($scope, $filter) {
//$scope.testDate = $filter('date')(Date.now(), 'yyyy/MM/dd');
$scope.testDate = new Date('2017-05-03');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
<input type="date" ng-model="testDate">
</div>
这篇关于AngularJS - 初始化DATE输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!