how to bind Bootstrap-datepicker element with angularjs ng-model?

后端 未结 11 667
囚心锁ツ
囚心锁ツ 2020-11-30 04:44

Here is the html for the date field :

相关标签:
11条回答
  • 2020-11-30 05:22

    im using this , and my code :

        this.todayNow = function () {
            var rightNow = new Date();
            return rightNow.toISOString().slice(0,10);
        };
        $scope.selecteddate = this.todayNow();
    
        $(function() {
            //for displaying datepicker
            $('.date').datepicker({
                format: 'yyyy-mm-dd',
                language:'fa'
            });
            //for getting input value
            $('.date').on("changeDate", function() {
                $scope.selecteddate = $(".date").val();
            });
    
        });
    
    0 讨论(0)
  • 2020-11-30 05:24

    One of ways around: set the id field to the input, then call document.getElementById('input_name').value to get the value of the input field.

    0 讨论(0)
  • 2020-11-30 05:27

    To solve this, I have my HTML looking like this:

    <div class='input-group date' id='datetimepicker1'>
    <input type='text' ng-model="date.arrival" name="arrival" id="arrival" 
         class="form-control" required />
         <span class="input-group-addon">
             <span class="glyphicon glyphicon-calendar"></span>
         </span>
    </div>
    

    My ng-model wouldn't update, so I used this to fix it:

    $("#datetimepicker1").on("dp.change", function (e) {
        $scope.date.arrival = $("#arrival").val(); // pure magic
        $('#datetimepicker2').data("DateTimePicker").minDate(e.date); // this line is not relevant for this example
    });
    
    0 讨论(0)
  • 2020-11-30 05:31

    Using this datepicker I had the same problem. I solved it using a little trick.

    In the inputs tag I added a new attribute (dp-model):

    <input class="form-control dp" type="text" dp-model="0" />
    <input class="form-control dp" type="text" dp-model="1" />
    ...
    

    And then in the js file I forced the binding in this way:

    $scope.formData.dp = []; // arrays of yours datepicker models
    $('.dp').datepicker().on('changeDate', function(ev){
        $scope.formData.dp[$(ev.target).attr('dp-model')] = $(ev.target).val();
    });
    
    0 讨论(0)
  • 2020-11-30 05:33

    Have you tried AngularUI bootstrap? It has all the bootstrap modules rewritten in angular(including datepicker): http://angular-ui.github.io/bootstrap/

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