角度时间选择器没有错误?

时间:2015-09-14 17:23:28

标签: javascript jquery angularjs

我在预订后使用时间选择器预订我已经存储在database.in编辑预订我已经不时输入字段但我收到错误

Error: [ngModel:datefmt] Expected `01:00:00 pm` to be a date
http://errors.angularjs.org/1.3.13/ngModel/datefmt?p0=01%3A00%3A00%20pm
    at REGEX_STRING_REGEXP (angular.js:63)
    at Array.<anonymous> (angular.js:19807)
    at Object.ngModelWatch (angular.js:23289)
    at Scope.promises.$get.Scope.$digest (angular.js:14235)
    at Scope.promises.$get.Scope.$apply (angular.js:14506)
    at done (angular.js:9659)
    at completeRequest (angular.js:9849)
    at XMLHttpRequest.requestLoaded (angular.js:9790).i have enclosed my code

.controller('EditCtrl', [
        '$scope', '$http', '$location', '$window', '$filter', '$ionicPopup', '$ionicLoading', '$timeout',
        function($scope, $http, $location, $window, $filter, $ionicPopup, $ionicLoading, $timeout) {
  $http.get('**').success(function(data, dealers, response) {
   // $scope.booktime = data.Scheduled_Time;
    //console.log($scope.booktime);
    $scope.booktime="01:00:00 pm";
})
  
}
    ]);
  <div ng-controller="EditCtrl">
    <div class="col">
            <label class="item item-input item-stacked-label">
            <span class="input-label" >Time</span>
            <input type="time" ng-model="booktime"  name="booktime" min="09:00:00" max="21:00:00"required="">
            </label>
            <div class="form-error" ng-messages="projectForm.booktime.$error">
               <div class="form-error" ng-message="required">* Mandatory</div>
              
               <div class="form-error" ng-message="max">Booking times: 9am - 9 pm</div>
            </div>
         </div>
  </div>

2 个答案:

答案 0 :(得分:1)

你必须以$scope.booktime = new Date(dateString)的形式使用。日期/时间输入的ng-model需要是有效的日期对象,因此需要转换为有效的日期对象

dt = new Date();
var time = dt.setHours(1) + ":" + dt.setMinutes(10) + ":" + dt.setSeconds(00);
var date= dt.getFullYear() + "/" + dt.getMonth() + "/" +dt.getDay() + " ";

$scope.booktime =new Date(date+ time;)

答案 1 :(得分:0)

你应该像下面的代码一样,它会给你正确的日期。

  var date_yours= new Date(); 
  date_yours.setHours(1);
  date_yours.setMinutes(10);
 console.log(date_yours);

和格式化日期(在您的情况下,您只需要时间)。我已经评论过am / pm并使用24小时格式

function formatDate(date) {
      var hours = date.getHours();
      var minutes = date.getMinutes();
    /*  var ampm = hours >= 12 ? 'pm' : 'am';
      hours = hours % 12;
      hours = hours ? hours : 12; // the hour '0' should be '12' */
      hours = hours < 10 ? '0'+hours : hours;
      minutes = minutes < 10 ? '0'+minutes : minutes; 
      var strTime = hours + ':' + minutes;
      //var strTime = hours + ':' + minutes + ' ' + ampm;


      return strTime ;

    }