jquery – 在Angular指令中使用bootstrap-daterangepicker

前端之家收集整理的这篇文章主要介绍了jquery – 在Angular指令中使用bootstrap-daterangepicker前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Angular指令中使用bootstrap-daterangepicker遇到麻烦.最初加载页面时工作正常,但是当切换页面/状态(我使用ui-router)并返回到带有daterangepicker的页面时,该插件不再工作.

它会抛出这个错误

TypeError: $(…).daterangepicker is not a function

看起来插件在更改状态时已经删除了.任何人一个想法如何解决这个问题?

  1. app.directive("daterangepicker",function() {
  2. return {
  3. restrict: "A",scope: false,link: function($scope,$element,$attr){
  4.  
  5. $($element).daterangepicker({
  6. format: "DD.MM.YYYY",startDate: moment().subtract('days',1),endDate: new Date(),buttonClasses: ['btn-primary'],},function(from,to) {
  7. $scope.date = {from: from,to: to};
  8. $scope.$apply(); // I need apply() here to use the two-way-databinding
  9. });
  10. }
  11. }
  12. });

解决方法

我会避免轮转轮胎,已经有几个项目提供的指令,使用 https://angular-ui.github.io/bootstrap/

导入模块:

angular.module(‘myModule’,[‘ui.bootstrap’]);

并使用它:

  1. <!doctype html>
  2. <html ng-app="ui.bootstrap.demo">
  3.  
  4. <head>
  5. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
  6. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
  7. <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.2.js"></script>
  8. <script src="example.js"></script>
  9. <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  10. </head>
  11.  
  12. <body>
  13.  
  14. <style>
  15. .full button span {
  16. background-color: limegreen;
  17. border-radius: 32px;
  18. color: black;
  19. }
  20.  
  21. .partially button span {
  22. background-color: orange;
  23. border-radius: 32px;
  24. color: black;
  25. }
  26. </style>
  27. <div ng-controller="DatepickerPopupDemoCtrl">
  28. <pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>
  29.  
  30. <h4>Popup</h4>
  31. <div class="row">
  32. <div class="col-md-6">
  33. <p class="input-group">
  34. <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" />
  35. <span class="input-group-btn">
  36.  
  37. <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
  38.  
  39. </span>
  40. </p>
  41. </div>
  42.  
  43. <div class="col-md-6">
  44. <p class="input-group">
  45. <input type="text" class="form-control" uib-datepicker-popup ng-model="dt" is-open="popup2.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" />
  46. <span class="input-group-btn">
  47.  
  48. <button type="button" class="btn btn-default" ng-click="open2()"><i class="glyphicon glyphicon-calendar"></i></button>
  49.  
  50. </span>
  51. </p>
  52. </div>
  53. </div>
  54. <div class="row">
  55. <div class="col-md-6">
  56. <label>Format: <span class="muted-text">(manual alternate <em>{{altInputFormats[0]}}</em>)</span></label>
  57. <select class="form-control" ng-model="format" ng-options="f for f in formats">
  58. <option></option>
  59. </select>
  60. </div>
  61. </div>
  62.  
  63. <hr />
  64. <button type="button" class="btn btn-sm btn-info" ng-click="today()">Today</button>
  65. <button type="button" class="btn btn-sm btn-default" ng-click="setDate(2009,7,24)">2009-08-24</button>
  66. <button type="button" class="btn btn-sm btn-danger" ng-click="clear()">Clear</button>
  67. <button type="button" class="btn btn-sm btn-default" ng-click="toggleMin()" uib-tooltip="After today restriction">Min date</button>
  68. </div>
  69. </body>
  70.  
  71. </html>

控制器:

  1. angular.module('ui.bootstrap.demo',['ngAnimate','ui.bootstrap']);
  2. angular.module('ui.bootstrap.demo').controller('DatepickerPopupDemoCtrl',function($scope) {
  3. $scope.today = function() {
  4. $scope.dt = new Date();
  5. };
  6. $scope.today();
  7.  
  8. $scope.clear = function() {
  9. $scope.dt = null;
  10. };
  11.  
  12. $scope.inlineOptions = {
  13. customClass: getDayClass,minDate: new Date(),showWeeks: true
  14. };
  15.  
  16. $scope.dateOptions = {
  17. dateDisabled: disabled,formatYear: 'yy',maxDate: new Date(2020,5,22),startingDay: 1
  18. };
  19.  
  20. // Disable weekend selection
  21. function disabled(data) {
  22. var date = data.date,mode = data.mode;
  23. return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
  24. }
  25.  
  26. $scope.toggleMin = function() {
  27. $scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
  28. $scope.dateOptions.minDate = $scope.inlineOptions.minDate;
  29. };
  30.  
  31. $scope.toggleMin();
  32.  
  33. $scope.open1 = function() {
  34. $scope.popup1.opened = true;
  35. };
  36.  
  37. $scope.open2 = function() {
  38. $scope.popup2.opened = true;
  39. };
  40.  
  41. $scope.setDate = function(year,month,day) {
  42. $scope.dt = new Date(year,day);
  43. };
  44.  
  45. $scope.formats = ['dd-MMMM-yyyy','yyyy/MM/dd','dd.MM.yyyy','shortDate'];
  46. $scope.format = $scope.formats[0];
  47. $scope.altInputFormats = ['M!/d!/yyyy'];
  48.  
  49. $scope.popup1 = {
  50. opened: false
  51. };
  52.  
  53. $scope.popup2 = {
  54. opened: false
  55. };
  56.  
  57. var tomorrow = new Date();
  58. tomorrow.setDate(tomorrow.getDate() + 1);
  59. var afterTomorrow = new Date();
  60. afterTomorrow.setDate(tomorrow.getDate() + 1);
  61. $scope.events = [{
  62. date: tomorrow,status: 'full'
  63. },{
  64. date: afterTomorrow,status: 'partially'
  65. }];
  66.  
  67. function getDayClass(data) {
  68. var date = data.date,mode = data.mode;
  69. if (mode === 'day') {
  70. var daytocheck = new Date(date).setHours(0,0);
  71.  
  72. for (var i = 0; i < $scope.events.length; i++) {
  73. var currentDay = new Date($scope.events[i].date).setHours(0,0);
  74.  
  75. if (daytocheck === currentDay) {
  76. return $scope.events[i].status;
  77. }
  78. }
  79. }
  80.  
  81. return '';
  82. }
  83. });

plunker:http://plnkr.co/edit/LZgUAZQkNKgDrUZAVWSa?p=preview

猜你在找的jQuery相关文章