Muhammad Binladen Muhammad Binladen - 4 months ago 8
AngularJS Question

Error: undefined is not an object?

I am having trouble figuring out what's the error here. Can anyone help me? I'm new to Angular.
Error: undefined is not an object (evaluating 'Logging.initLogComponent')

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 }]);

Answer

You havent added Logging dependency correctly in your controller

Change your controller to this:

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

Notice how I've passed one extra parameter 'Logging' in your controller. It is different from the the parameter that's passed in your function.

Comments