【问题标题】:Why is my AngularJS ngModel binding to the time inputs, but not binding to the date inputs?为什么我的 AngularJS ngModel 绑定到时间输入,但没有绑定到日期输入?
【发布时间】:2013-11-25 18:33:02
【问题描述】:

我正在尝试将date 类型的input 绑定到模型。我能够绑定到time 字段,但我遇到date 字段的问题。 HTML:

<div ng-app ng-controller="HistoryCtrl">
    <input type="date" nm-model="startDate" />
    <input type="time" ng-model="startTime" />
    <input type="date" nm-model="endDate" />
    <input type="time" ng-model="endTime" />
    <button ng-click="updateForm()">Update</button>
</div>

这是我的控制器(简化版):

function HistoryCtrl($scope) {

    $scope.result = {
        result: 'success',
        start: '2013-11-23 03:00:00',
        end: '2013-11-24 16:30:00',
        delta: 0.05681799352169
    };

    $scope.updateForm = function () {
        $scope.updateTimespan($scope.result.start, $scope.result.end);
    };

    $scope.updateTimespan = function (start, end) {
        $scope.startDate = start.split(" ")[0];
        $scope.startTime = start.split(" ")[1];
        $scope.endDate = end.split(" ")[0];
        $scope.endTime = end.split(" ")[1];
    }
}

这是一个小提琴:http://jsfiddle.net/t3m6r/2/

我正在使用适用于 Mac 的 Google Chrome 31.0.1650.57。当我单击“更新”按钮时,time 字段会更新,但date 字段不会。为什么?我是doing it wrong吗?

【问题讨论】:

  • 也许是因为nm-model 对 Angular 没有任何意义。
  • 这个问题似乎是题外话,因为它是关于错字的。

标签: javascript angularjs date data-binding


【解决方案1】:

您正在使用 ng-model 但输入错误,“nm-model”。

<input type="date" ng-model="startDate" />
<input type="time" ng-model="startTime" />
<input type="date" ng-model="endDate" />
<input type="time" ng-model="endTime" />

JS Fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    相关资源
    最近更新 更多