2016-07-21 70 views
1

我無法弄清楚這裏有什麼錯誤。誰能幫我?我是Angular的新手。 錯誤:不確定是不是(評估「Logging.initLogComponent」)的對象錯誤:undefined不是一個對象?

14 app.factory('Logging', function($fileLogger, $filter) { 
15 var today = new Date(); 
16 today = $filter('date')(today, 'yyyy-MM-dd'); 
17 
18 
19 return { 
20  initLogComponent : function() { 
21  fileName = today;  
22  $fileLogger.setStorageFilename(fileName); 
23  console.log('Current log file: ' + fileName); 
24  $fileLogger.setTimestampFormat('medium'); 
25  } 
26 }; 
27 }); 
28 
29 app.controller('loggingController', ['$scope', '$fileLogger', '$timeout', function($scope, $fileLogger, $timeout, Logging){                    
30 Logging.initLogComponent(); 
31 
32 
33 }]); 
+1

不完全相同的代碼,但這是你的問題http://stackoverflow.com/questions/28893148/angularjs-parse-typeerror-object-is-not-a-function/28893229#28893229 – Claies

回答

0

你還沒有在你的控制器Logging依賴正確添加

控制器改成這樣:

app.controller('loggingController', ['$scope', '$fileLogger', '$timeout', 'Logging', function($scope, $fileLogger, $timeout, Logging){                    
    Logging.initLogComponent(); 
}]); 

注意我已經在控制器中傳遞了一個額外的參數'Logging'它與您的函數中傳遞的參數不同。

+0

感謝一個很好! ! –

+0

嘿,每當我添加一個默認參數參數給函數initLogComponent,我得到這個錯誤消息:參數'loggingController'不是一個函數,得到了undefined?例如initLogComponent:function(fileName = today) –

+0

嘿,這裏有什麼問題? –

0

您正在嘗試使用DateTime類型的字符串中的不首先它強制轉換爲字符串,請嘗試:

var stringDate = today.ToString(); 
相關問題