如何在控制器内设置配置

时间:2014-11-11 07:14:49

标签: javascript angularjs

我在申请中写作提供者如下;

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变为null并且没有任何结果。我知道我的使用错误,但仍然不知道使用服务或提供商或工厂的正确方法。请告诉我有关设置和检索的正确方法。

1 个答案:

答案 0 :(得分:0)

在引导阶段后无法配置模块。配置需要在之前发生。您必须在控制器的范围之外执行此操作。