Angularjs和Jquery日期选择器数据绑定

Angularjs and Jquery datepicker data-binding

本文关键字:选择器 数据绑定 日期 Jquery Angularjs      更新时间:2024-04-02

我用日期选择器(JQuery)做了一个项目。当我在日期选择器中单击日期时,它从不显示日期,除非键入空格或enter。我希望当我点击日期时,点击的日期会显示出来。

我希望在日期选择器中单击日期时,输出会自动显示日期。但在它生成/显示日期之前,我必须先按空格键。。请帮忙。。这些是我的代码:

  $(document).ready(function () {
                $("#datepickerfrom").datepicker({
                    numberOfMonths: 1,
                    onSelect: function (selected) {
                        $("#datepickerto").datepicker("option", selected)
                    }
                });
                $("#datepickerto").datepicker({
                    numberOfMonths: 1,
                    onSelect: function (selected) {
                        $("#datepickerfrom").datepicker("option", selected)
                    }
                });
               //  jQuery object; this demo
            });
            function GetbyDate(fr, t) {
                var from = new Date(t)
            };
            scope.changeDate = function () {
                scope.models = null;
                http.get('GetReportList?from=' + scope.filter_fromDate + '&to=' + scope.filter_toDate).success(
                    function (data) {
                        scope.models = data;
                    });
            }
              <p class="input-group">
                                <input type="text" class="form-control" id="datepickerfrom" data-ng-model="filter_fromDate" />
                                
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-default">
                                        <i class="glyphicon glyphicon-calendar"></i>
                                    </button>
                                </span>
                                {{filter_fromDate}}
                            </p>
                            <p class="input-group">
                                <input type="text" class="form-control" id="datepickerto" data-ng-model="filter_toDate" />
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-default">
                                        <i class="glyphicon glyphicon-calendar"></i>
                                    </button>
                                </span>
                                {{filter_toDate}}
                            </p>

是!我得到了答案。。

感谢这个示例jsfiddle答案。我发现我不能显示日期的原因是我把它放在了控制器里。

看看这个:

var PRApp = angular.module('PRApp', []);
        PRApp.controller('PRCtrl', ['$scope', '$http', function (scope, http) {
            http.get('GetList').success(function (data) {
                scope.models = data;
                scope.sorting = "-PRDate";
                var i = 0;
                for (i = 0; i < scope.models.length; i++) {
                    scope.models[i].id = i;
                }
            });
            scope.getStatus = http.get('GetStatusList').success(function (status) {
                scope.StatusList = status
            });
            function GetbyDate(fr, t) {
                var from = new Date(t)
            };
            scope.changeDate = function () {
                scope.models = null;
                http.get('GetReportList?from=' + scope.filter_fromDate + '&to=' + scope.filter_toDate).success(
                    function (data) {
                        scope.models = data;
                    });
            }
            scope.jsonDatetotext = function (jsondate) {
                // jsondate format: /Date(#############)/
                // substr(6) will remove '/Date('
                // parseInt will convert remaing string '#############' to int and ignores ')/'
                return new Date(parseInt(jsondate.substr(6)));
            };
        }]);
        PRApp.directive('calendar2', function () {
            return {
                require: 'ngModel',
                link: function (scope, el, el2, attr, ngModel) {
                    attr.$observe("show", function (val) {
                        if (val == 'true') {
                            $(el).datepicker("show");
                        }
                        else {
                            $(el).datepicker("hide");
                        }
                    });
                    attr.$observe("show", function (val) {
                        if (val == 'true') {
                            $(el2).datepicker("show");
                        }
                        else {
                            $(el2).datepicker("hide");
                        }
                    });
                    $(el).datepicker({
                        minDate: '-5Y',
                        dateFormat: 'MM d, yy',
                        onSelect: function (dateText) {
                            scope.$apply(function () {
                                ngModel.$setViewValue(dateText);
                            });
                        }
                    });
                    $(el2).datepicker({
                        minDate: '-5Y',
                        dateFormat: 'MM d, yy',
                        onSelect: function (dateText) {
                            scope.$apply(function () {
                                ngModel.$setViewValue(dateText);
                            });
                        }
                    });
                }
            };
        });
<span class="input-group-addon">
                                    <span data-ng-click="show=!show" class="glyphicon glyphicon-calendar"></span>
                                </span>
                                <input data-show="{{show}}" type="text" name="filter_fromDate" calendar2 data-ng-model="filter_fromDate"
                                    class="form-control" placeholder="mm/dd/yyyy" data-ng-minlength="10" />
                                 </div>
                                                    <br />
                        <div class="input-group">
                                <span class="input-group-addon">
                                    <span data-ng-click="show=!show" class="glyphicon glyphicon-calendar"></span>
                                </span>
                                <input data-show="{{show}}" type="text" name="filter_toDate" calendar2 data-ng-model="filter_toDate"
                                    class="form-control" placeholder="mm/dd/yyyy" data-ng-minlength="10" />
  
       
                             </div>   
                           <br />
                             <input type="submit" class="btn btn-primary btn-sm" value="GO" />
                        </div>

像这样的代码应该始终进入指令的链接函数。 $(document).ready(function () {/**/} ^--你根本不应该使用这个。

一种方法可能是:

angular
  .module('app', [])
  .directive('thirdparty', jQueryDirective)
function jQueryDirective(){
  return {
    restrict: 'E',
    template: '<div id="foo"></div>',
    link: function(scope, element, attrs){
      $(element).append('appended with jquery')
    }
  }
}
angular.bootstrap(document, ['app'])
#foo {
  width: 100px;
  height: 100px;
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<thirdparty/>