Angular.js and HTML5 date input value — how to get Firefox to show a readable date value in a date input?

前端 未结 7 1980
無奈伤痛
無奈伤痛 2020-12-12 13:26

I have an HTML5 date input and I would like its value to be set to the value of the date property in my model by default. I\'m not too fussy about formatting since Chrome se

相关标签:
7条回答
  • 2020-12-12 14:29

    I've used ng-change:

    Date.prototype.addDays = function(days) {
      var dat = new Date(this.valueOf());
      dat.setDate(dat.getDate() + days);
      return dat;
    }
    
    var app = angular.module('myApp', []);
    
    app.controller('DateController', ['$rootScope', '$scope',
      function($rootScope, $scope) {
        function init() {
          $scope.startDate = new Date();
          $scope.endDate = $scope.startDate.addDays(14);
        }
    
    
        function load() {
          alert($scope.startDate);
          alert($scope.endDate);
        }
    
        init();
        // public methods
        $scope.load = load;
        $scope.setStart = function(date) {
          $scope.startDate = date;
        };
        $scope.setEnd = function(date) {
          $scope.endDate = date;
        };
    
      }
    ]);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div data-ng-controller="DateController">
      <label class="item-input"> <span class="input-label">Start</span>
        <input type="date" data-ng-model="startDate" ng-change="setStart(startDate)" required validatedateformat calendar>
      </label>
      <label class="item-input"> <span class="input-label">End</span>
        <input type="date" data-ng-model="endDate" ng-change="setEnd(endDate)" required validatedateformat calendar>
      </label>
      <button button="button" ng-disabled="planningForm.$invalid" ng-click="load()" class="button button-positive">
        Run
      </button>
    </div <label class="item-input"> <span class="input-label">Start</span>
    <input type="date" data-ng-model="startDate" ng-change="setStart(startDate)" required validatedateformat calendar>
    </label>
    <label class="item-input"> <span class="input-label">End</span>
      <input type="date" data-ng-model="endDate" ng-change="setEnd(endDate)" required validatedateformat calendar>
    </label>

    0 讨论(0)
提交回复
热议问题