I try to inject $timeout in the run
function but I get that it is not a function
when I try to call it. Why ?
var mainApp = angular.module('mainApp', ['ngRoute', 'ngAnimate', 'ui.bootstrap', ngCookies']);
mainApp.run(['$rootScope', '$location', '$timeout'
function ($rootScope, $location, $route, authService, $timeout) {
...
}]);
This '$timeout' service of AngularJS is functionally similar to the 'window. setTimeout' object of vanilla JavaScript. This service allows the developer to set some time delay before the execution of the function.
The $timeout service can be used to call another JavaScript function after a given time delay. The $timeout service only schedules a single call to the function. For repeated calling of a function, see $interval later in this text.
We can use the setTimeout() function for various reasons. It is best to allow Angular to run change detection once, between the actions that we would otherwise perform synchronously.
var customTimeout = $timeout(function () { // arbitrary code }, 55); $timeout. cancel(customTimeout); The same applies to “$interval()”. To disable a watch, just call it.
mainApp.run(['$rootScope', '$location', '$timeout'
function ($rootScope, $location, $route, authService, $timeout) {
...
}]);
should be:
mainApp.run(['$rootScope', '$location', '$route', 'authService', '$timeout',
function ($rootScope, $location, $route, authService, $timeout) {
...
}]);
see 'The array annotation' part here:
https://docs.angularjs.org/api/auto/service/$injector
When you annotate function with names of dependencies, the order of appearance should match.
...
mainApp.run(['$rootScope', '$location', '$route', '$timeout', 'authService',
function ($rootScope, $location, $route, $timeout, authService) {
...
}]);
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