2016-05-20 14 views
0

ionicDatePickerとionicTimePickerを2つの異なるコントローラで使用していますが、コードの再利用が多いことに気付きました。どうすればこれを減らすことができますか?コントローラ内のionicDatePickerとionicTimePickerのコード複製を減らすためのリファクタリング

これは、これまでの私のコードです:私はむしろionicDatePickerとionicTimePickerを使用したい

function ConsumerBusinessProfileCtrl($scope, ionicDatePicker, ionicTimePicker, DatePickerService, TimePickerService) { 

    // Booking time and date 

    $scope.booking = { 
     'date': DatePickerService.getDate(), 
     'time': TimePickerService.getTime() 
    } 


    // Ionic datepicker 

    $scope.openDatePicker = function(val) { 

     var datePickerObject = { 

     callback: function(val) { 

      console.log('Return value from the datepicker popup is : ' + val, new Date(val)); 

      // Set new date 

      var selectedDate = new Date(val); 
      var dd = selectedDate.getDate(); 
      var mm = selectedDate.getMonth() + 1; 
      var yyyy = selectedDate.getFullYear(); 

      if (dd < 10) { 
      dd = '0' + dd 
      } 

      if (mm < 10) { 
      mm = '0' + mm 
      } 

      $scope.booking.date = dd + '/' + mm + '/' + yyyy; 
     }, 

     from: new Date(), 
     inputDate: new Date(), 
     closeOnSelect: false, 
     templateType: 'popup', 
     weeksList: ["S", "M", "T", "W", "T", "F", "S"], 
     }; 

     ionicDatePicker.openDatePicker(datePickerObject); 

    }; 


    // Ionic timepicker 

    $scope.openTimePicker = function() { 

     var timePickerObject = { 

     step: 15, //Optional 

     callback: function(val) { 

      if (typeof(val) === 'undefined') { 
      console.log('Time not selected'); 
      } else { 
      var selectedTime = new Date(val * 1000); 
      console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), 'H :', selectedTime.getUTCMinutes(), 'M'); 
      } 

      // Set new time 

      var selectedTime = new Date(val * 1000); 
      var h = selectedTime.getUTCHours() 
      var m = selectedTime.getUTCMinutes() 

      $scope.booking.time = h + ":" + ("00" + m).slice(-2); 

     }, 

     inputTime: TimePickerService.getRoundedTime(), 
     format: 24 

     }; 

     ionicTimePicker.openTimePicker(timePickerObject); 

    } 

function BusinessAddAppointmentCtrl($scope, ionicDatePicker, ionicTimePicker, DatePickerService, TimePickerService) { 

    // Booking time and date 

    $scope.booking = { 
    'date': DatePickerService.getDate(), 
    'time': TimePickerService.getTime() 
    } 


    // Ionic datepicker 

    $scope.openDatePicker = function(val) { 

    var datePickerObject = { 

     callback: function(val) { 

     console.log('Return value from the datepicker popup is : ' + val, new Date(val)); 

     // Set new date 

     var selectedDate = new Date(val); 
     var dd = selectedDate.getDate(); 
     var mm = selectedDate.getMonth() + 1; 
     var yyyy = selectedDate.getFullYear(); 

     if (dd < 10) { 
      dd = '0' + dd 
     } 

     if (mm < 10) { 
      mm = '0' + mm 
     } 

     $scope.booking.date = dd + '/' + mm + '/' + yyyy; 
     }, 

     from: new Date(), 
     inputDate: new Date(), 
     closeOnSelect: false, 
     templateType: 'popup', 
     weeksList: ["S", "M", "T", "W", "T", "F", "S"], 
    }; 

    ionicDatePicker.openDatePicker(datePickerObject); 

    }; 


    // Ionic timepicker 

    $scope.openTimePicker = function() { 

    var timePickerObject = { 

     step: 15, 

     callback: function(val) { 

     if (typeof(val) === 'undefined') { 
      console.log('Time not selected'); 
     } else { 
      var selectedTime = new Date(val * 1000); 
      console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), 'H :', selectedTime.getUTCMinutes(), 'M'); 
     } 

     // Set new time 

     var selectedTime = new Date(val * 1000); 
     var h = selectedTime.getUTCHours() 
     var m = selectedTime.getUTCMinutes() 

     $scope.booking.time = h + ":" + ("00" + m).slice(-2); 

     }, 

     inputTime: TimePickerService.getRoundedTime(), 
     format: 24 

    }; 

    ionicTimePicker.openTimePicker(timePickerObject); 

    } 

} 

答えて

1

使用コントローラで

var options = { 
    date: new Date(), 
    mode: 'date' 
}; 

function onSuccess(date) { 
    alert('Selected date: ' + date); 
} 

function onError(error) { // Android only 
    alert('Error: ' + error); 
} 

datePicker.show(options, onSuccess, onError); 
+0

以下cordova datePicker Plugin代わりに、非常に単純な、例。それは可能ですか? – methuselah

+0

次に、urcodeを減らすjavascriptの日付ライブラリであるmoment.jsを使用します – rashidnk

関連する問題