输入以angularjs中的格式(mm:ss)捕获分钟和秒

时间:2017-11-13 17:48:04

标签: html angularjs

我正在为angularjs中的应用程序创建一个简单的表单。我需要一个输入字段来接收时间数据,只需几分钟和几秒,格式为“mm:ss”。任何为此工作过的人都会非常感谢您的帮助。我已经为表单创建了时间输入。实际上,我正在研究由其他程序员创建的项目。时间输入的模型初始化如下:

function initDate(date, indexX, indexY){
      if(date != null && date != ''){
        var dateArray = date.split(":");
        var dt = new Date(1970,1,1,dateArray[0],dateArray[1],dateArray[2]);
        vm.modelDate[indexX][indexY] = dt;
      }
}

<input name="inputExTime{{q.listSubQuestions[contTime].questionId}}{{contTime}}0"
    type="time" class="form-control btn-act btn-act-inputs intime_ex" step="1"
    ng-disabled="disabledfields"
    ng-model="vm.modelDate[0][contTime]"
    min="00:00:00" max="12:59:00"
    ng-change="vm.changeDate(parentIndex,0,contTime);vm.calculateHours(form['inputExTime'+q.listSubQuestions[contTime].questionId+contTime+0].$modelValue, form['inputExTime'+q.listSubQuestions[contTime].questionId+contTime+2].$modelValue, form['inputExTime'+q.listSubQuestions[contTime].questionId+contTime+3], contTime);vm.average();"
    ng-required="$index<vm.numberOfRequiredRows?(q.attributes.required==false?false:isregister):false"
    ng-class="{true: 'inp-f'}[form['inputExTime'+q.listSubQuestions[contTime].questionId+contTime+0].$invalid]"
    ng-init="vm.modelDate[0][contTime]=null;vm.initArray(''+q.listSubQuestions[contTime].questionId+contTime+0);vm.initDate(q.listSubQuestions[0].listAnswers[contTime].detail, 0, contTime);"

/&GT;

1 个答案:

答案 0 :(得分:0)

您需要以时间input的形式定义time元素:

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

ng-model将是Date对象。否则,AngularJS将抛出错误。

&#13;
&#13;
var app = angular.module('time', []);

app.controller('MainCtrl', function($scope) {
  // The order of the params is the following:
  // Year, Month, Day, Time, Minutes, Seconds
  $scope.timeModel = new Date(2017, 10, 13, 10, 30, 0);
});
&#13;
<!DOCTYPE html>
<html ng-app="time">

<head>
  <meta charset="utf-8" />
  <title>Time Model Example using AngularJS</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <input type="time" ng-model="timeModel">
</body>

</html>
&#13;
&#13;
&#13;

official documentation上,您可以找到有关此内容的更多信息。

继续摇滚!