如何从html输入类型="时间"中获取Angular控制器变量中的唯一时间值

时间:2016-04-08 14:16:59

标签: javascript html angularjs datetimepicker

我有html输入:

 <input type="time" id="exampleInput" name="input" ng-model="ctrl.time"
               placeholder="HH:mm:ss" min="00:00:00" max="24:00:00" required />

根据我的意见,我可以将这样的内容输入我的&c; ctrl.time&#39;:

"1970-01-01T02:04:00.000Z"

但我希望只有时间:14:03:00

是否有解决方案使数据在转到Angular变量之前进行格式化或在内部格式化?

提前感谢您的回答。

1 个答案:

答案 0 :(得分:1)

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

app.controller('main', function($scope) {
  $scope.ctrl = {};
  $scope.show = function(){
    console.log( $scope.ctrl.time);
    console.log(moment($scope.ctrl.time).format('HH:mm:ss'));
  };
});
&#13;
<html>
  <head>
    <script data-require="moment.js@*" data-semver="2.10.2" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js"></script>    
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>    
  </head>

  <body ng-app="plunker" ng-controller="main">
    <input type="time" id="exampleInput" name="input" 
          ng-model="ctrl.time" 
          ng-change="show()" 
          placeholder="HH:mm:ss" min="00:00:00" max="24:00:00" required="" />
    
  </body>
</html>
&#13;
&#13;
&#13;