I wrote a directive and a service. The directive calls a timeout function in the service. However, once the $timeout is reached, it throws an error:
TypeError: object is not a function
Original $scope.msg
is not displayed. And the $timeout
function does not wait for (20sec) to call the callback (or so I assume, since the $scope.msg
changes right away).
I am absolutely lost. I found a few questions regarding timeouts, but none of them seem to have this problem. This one is as close as I got, and I am already following that answer Angularjs directive TypeError: object is not a function.
Here is a Plunker to the code: http://plnkr.co/edit/xrepQOWIHlccbW5atW88?p=preview
And here is the actual code.
angular.module('myApp', [
'ui.bootstrap',
'myApp.services',
'myApp.directives',
]);
angular.module('myApp.directives', []).
directive('logoutNotification', [
function(admin) {
return {
restrict: "E",
template: "<div>{{msg}}</div>",
controller: ["$scope", "$timeout", "admin",
function($scope, $timeout, admin) {
$scope.msg = "Hey, no timeout yet";
$scope.resetNotification = function() {
admin.resetNoticeTimer(function(){
$scope.msg = "Hey, I'm back from timeout"
});
};
}
],
link: function(scope) {
scope.resetNotification();
}
};
}
]);
angular.module('myApp.services', []).
factory('admin', ['$rootScope', '$location', '$timeout',
function($rootScope, $timeout, $location, admin) {
var noticeTimer;
return {
resetNoticeTimer: function(cb) {
if (noticeTimer) {
$timeout.cancel(noticeTimer);
}
noticeTimer = $timeout(cb(), 20000);
}
};
}
]);
Thanks in advance!
You have misordered the dependencies.
Your code:
factory('admin', ['$rootScope', '$location', '$timeout',
function($rootScope, $timeout, $location, admin) {
Should be:
factory('admin', ['$rootScope', '$timeout', '$location',
function($rootScope, $timeout, $location) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With