【问题标题】:Angularjs: Set a value of input of type dateAngularjs:设置日期类型的输入值
【发布时间】:2014-11-16 20:17:09
【问题描述】:

这可能是一个愚蠢而简单的问题,但我似乎无法解决这个问题。 我正在尝试设置 Date 类型的字段,但没有显示任何内容。这是我的代码:

在 HTML 中:

<input type="date" name="departure" ng-model="departure" placeholder="yyyy-MM-dd" required/>

在控制器中我做这样的事情:

$scope.departure = new Date(2015, 9, 22);

“出发”字段中没有显示任何内容。我不明白为什么。这似乎很容易。 我按照这个例子:https://docs.angularjs.org/api/ng/input/input%5Bdate%5D

请帮忙。我正在使用 Angular 1.3 谢谢

【问题讨论】:

  • 确保在 html 中设置了 ng-controller。

标签: javascript angularjs angularjs-scope


【解决方案1】:

我假设您只是想将您的范围变量偏离转换为日期输入的格式。我创建了这个plunkr

JS

var app = angular.module('stack',[]);

app.controller('DateController', ['$scope', function($scope){
    $scope.departure = new Date(2015, 10, 22);
}]);

HTML

<!DOCTYPE html>
<html ng-app="stack">

  <head>
    <script src="//code.angularjs.org/1.3.1/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="DateController">
    <input type="date" name="departure" ng-model="departure" placeholder="yyyy-MM-dd" required/>
  </body>

</html>

再次不确定您要做什么,但这对我有用。希望这会有所帮助。

【讨论】:

  • 附带说明:月份索引是从零开始的,所以new Date(2015, 10, 22) 是 2015 年 11 月 22 日。以防万一有人碰到这个。
猜你喜欢
  • 2019-10-04
  • 2016-05-27
  • 2014-09-24
  • 2015-12-15
  • 2020-01-02
  • 2020-03-25
  • 2021-07-30
  • 2022-12-05
  • 2019-01-27
相关资源
最近更新 更多