Я написал как поставщика в приложении, как показано ниже;Как настроить конфигурацию внутри контроллера
module.provider('CalculateTime', function() {
this.$get = function() {
var seconds = this.seconds;
var minutes = this.minutes;
var hours = this.hours;
var day = this.day;
return {
toSeconds: function(){
return Math.round(seconds);
},
toMinutes: function(){
return Math.round(minutes);
},
toHours: function(){
return Math.round(hours);
},
toDays: function(){
return Math.round(day);
},
exactDate: function(){
return Math.floor(hours%24)+":"+ Math.floor(minutes%60)+":"+ Math.floor(seconds%60);
}
}
};
this.setTime = function(milis) {
this.milis = milis;
this.seconds = this.milis/1000;
this.minutes = this.seconds/60;
this.hours = this.minutes/60;
this.day = this.hours/24;
};
});
и я хочу установить конфигурацию внутри контроллера (после завершения подготовки и настройки). Поэтому я попытался сделать следующее:
module.controller('ResultController',function($scope,$data,CalculateTime){
var date = $data.post.date;
given.setDate(date.selectedYear,date.selectedMonth,date.selectedDay);
var now = new DateTime();
var diff = now.compare(given);
// config here
module.config(function(CalculateTimeProvider){
CalculateTimeProvider.setTime(diff);
});
setTimeout(function() {
$scope.$apply(function() {
$scope.result = CalculateTime.toDays() + " Days " + CalculateTime.exactDate();
});
}, 100);
$ scope.result становится нулевым, и ничего не получается. Я знаю, что я ошибаюсь в использовании и до сих пор не знаю, как правильно использовать ли сервис или поставщик или завод. Расскажите, пожалуйста, о правильных способах настройки и извлечения.