简体   繁体   中英

Angularjs min max date

How to Set min-max of dates dynamically in angularjs. I mean if I set the min date in angularjs it should be binded automatically to frontend. this is what i wrote in my controller $scope.from1date='2016-12-31'; $scope.to1date='2016-12-31'; Whenver I change my min date in my controller it should be reflected in my frontendpage and how to do that...

It seems to work for me. Have you taken a look at this tutorial on angularjs controllers from W3Schools? I'm guessing your overall logic might somehow be wrong/buggy. Particularly, this code snippet from the tutorial is exactly what you need for the formatting (you could even copy-paste it to your source code) and put your exact code you provided in the comment of your post in it. Then you might notice where you've gone wrong.

I hope this helps – let me know how it goes!

EDIT: I see, here's where you went wrong. You need to use a Date object (see Date in the MDN Docs).

Simply, something like this should do it:

$scope.from1date= new Date(2015, 12, 20); //'2016-12-20';
$scope.to1date= new Date(2015, 12, 31); //'2016-12-31';

I am trying to give you simple example. Hope this helps. If this doesn't work, do let me know the specifics to check it further. I have fixed one date and the other using system day for a better example.

Controller code

 var app = angular.module('myApp', []);

    app.controller('myCtrl', function ($scope) {
        $scope.minDate = new Date("July 21, 1983 01:15:00");
        $scope.maxDate = new Date();
    });

HTML code

I have added date filter in one of the text box. You can add date filters as required.

 <head>
        <title></title>    
        <script src="../Scripts/angular.js"></script>
        <script src="../js/DateTest.js"></script>    
    </head>
    <body>
        <div ng-app="myApp" ng-controller="myCtrl">
            Min Date: <input type="text" ng-model="minDate | date: medium"><br>
            Max Date: <input type="text" ng-model="maxDate"><br>
            <br>        

        </div>

    </body>

结果如下

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM