Angular ionic 日期组件 带点击前一天 后一天的功能

前端之家收集整理的这篇文章主要介绍了Angular ionic 日期组件 带点击前一天 后一天的功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


angular结合 ionic 写的小时间组件

1、安装nodejs

2、通过命令进入指定盘下路径输入以下命令:npm install -g bower

3、运行bower install 安装对应的依赖JS库

4、通过命令进入指定盘下路径输入以下命令:npm run install-g(如果已经执行过该方法,直接跳过该步)
5、进入开发环境:
npm run serve-dev 开启开发环境服务

代码部分:
Css代码部分:

  1. /***按钮禁用样式和启用的样式******/
  2. .btnDisabled {
  3. background-color: #C0C0C0;
  4. }
  5. .btnEnabled {
  6. background-color: #0097FF;
  7. }
  8. /***搜索中的日期组件的样式*******/
  9. .month_select select,.year_select select{
  10. width: 40px;
  11. direction: ltr;
  12. margin: 0 auto;
  13. }
  14. .ionic_datepicker_popup .popup-buttons button,.ionic_datepicker_popup .selected_date,.ionic_datepicker_popup .popup-body .selected_date_full {
  15. background-color: #C29A2A!important;
  16. }
  17. .ionic_datepicker_popup .popup-body .month_select,.ionic_datepicker_popup .popup-body .year_select
  18. {
  19. border-bottom: 1px solid #C29A2A!important;
  20. margin-bottom: 1px;
  21. }
  22. .ionic_datepicker_popup .today {
  23. border: 1px solid #C29A2A!important;
  24. }
  25. .ionic_datepicker_popup .popup-body .button-clear,.ionic_datepicker_popup .popup-body .month_select:after,.ionic_datepicker_popup .popup-body .year_select:after
  26. {
  27. color: #C29A2A!important;
  28. }
  29. /***搜索中的日期组件的样式以上*******/
  30. //通用按钮样式
  31. button {
  32. background: #45B4FF;
  33. border-radius: 5px;
  34. color: #ffffff;
  35. width: 75px;
  36. height: 30px;
  37. border: 0;
  38. }
  39. .changeDate {
  40. width: 100%;
  41. padding: 10px 10px!important;
  42. height: 55px;
  43. margin-bottom: 10px;
  44. white-space: nowrap;
  45. }
  46. .changeDate .bg {
  47. width: 99%;
  48. background-color: #E8E8E8;
  49. }
  50. .changeDate span {
  51. text-align: center;
  52. display: inline-block;
  53. height: 29px;
  54. }
  55. .width33 {
  56. width: 33%;
  57. display: inline-block;
  58. line-height: 2;
  59. }

在 index.route.js 中添加日期组件的初始化

  1. (function() {
  2. 'use strict';
  3. angular
  4. .module('app')
  5. .config(routerConfig);
  6.  
  7. // routerConfig
  8. function routerConfig($stateProvider,$urlRouterProvider,$ionicConfigProvider,ionicDatePickerProvider) {
  9. var datePickerObj = {
  10. inputDate: new Date(),titleLabel: '选择日期',setLabel: '确定',todayLabel: '今天',closeLabel: '关闭',mondayFirst: false,weeksList: ['日','一','二','三','四','五','六'],monthsList: ['1','2','3','4','5','6','7','8','9','10','11','12'],templateType: 'popup',from: new Date(2009,1,1),to: new Date(2058,12,31),showTodayButton: true,dateFormat: 'yyyy-MM-dd',cloSEOnSelect: false,disableWeekdays: []
  11. };
  12. ionicDatePickerProvider.configDatePicker(datePickerObj);
  13. $stateProvider
  14. .state('list',{
  15. url: '/list',templateUrl: 'template/list.html',controller: 'ListController',controllerAs: 'vm'
  16. });
  17.  
  18. $urlRouterProvider.otherwise('/list');
  19. }
  20. })();

Html 代码

  1. <div class="white-bg changeDate"> <div class="bg"> <div class="width33" ng-model="yesterday" ng-click="yesterdayClick()"> <button>前一天</button> </div> <span class="width33" ng-model="dateValue" ng-click="dateOpen()">{{dateValue}}</span> <div class="width33" ng-model="tomorrow" ng-click="tomorrowClick()" style="text-align: right"> <button ng-disabled="vm.isDisabled" ng-class="{'true' : 'btnDisabled','false' : 'btnEnabled'}[vm.isDisabled]">后一天</button> </div> </div> </div>

JS 代码部分

  1. (function() {
  2. 'use strict';
  3. angular
  4. .module('app')
  5. .controller('ListController',ListController);
  6. ListController.$inject = ['$scope','ionicDatePicker','$filter'];
  7. function ListController($scope,ionicDatePicker,$filter) {
  8. var vm = this;
  9. var date = new Date();
  10. vm.maxDate = new Date(date.setDate(date.getDate() + 90));
  11. vm.today = $filter('date')(new Date(),'yyyy-MM-dd');
  12. vm.isDisabled = false;
  13. vm.changeDate = changeDate;
  14. vm.changeDate();
  15. //初始化时间组件
  16. function changeDate(dateFlag) {
  17. var startDateObj;
  18. if (dateFlag) {
  19. $scope.dateValue = $filter('date')(dateFlag,'yyyy-MM-dd');
  20. } else {
  21. $scope.dateValue = vm.today;
  22. }
  23. startDateObj = {
  24. //选择日期后的回调
  25. callback: function (val) {
  26. $scope.dateValue = $filter('date')(val,'yyyy-MM-dd');
  27. startDateObj.inputDate = new Date(val); //更新日期弹框上的日期
  28. },from: new Date(2010,to: vm.maxDate,inputDate: new Date($scope.dateValue),mondayFirst: true,templateType: 'popup'
  29. };
  30. //打开开始日期选择框
  31. $scope.dateOpen = function () {
  32. ionicDatePicker.openDatePicker(startDateObj);
  33. };
  34. }
  35. $scope.yesterdayClick = function () {
  36. var yDate = new Date($scope.dateValue);//获取当前时间
  37. yDate.setDate(yDate.getDate() - 1);//设置天数 -1 天
  38. vm.changeDate(yDate);
  39. vm.isDisabled = false;
  40. };
  41. $scope.tomorrowClick = function () {
  42. var tDate = new Date($scope.dateValue);//获取当前时间
  43. tDate.setDate(tDate.getDate() + 1);//设置天数 +1 天
  44. vm.changeDate(tDate);
  45. if ($scope.dateValue == $filter('date')(vm.maxDate,'yyyy-MM-dd')) {
  46. vm.isDisabled = true;
  47. }
  48. };
  49. }
  50. })();

猜你在找的Angularjs相关文章