Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - $timeout is not a function

Tags:

angularjs

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) {
...
}]);
like image 354
Tony Avatar asked Dec 03 '14 12:12

Tony


People also ask

What is $timeout in AngularJS?

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.

What is the function of $Timeout service?

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.

Can I use setTimeout in Angular?

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.

How do you reset a $timeout and disable a watch ()?

var customTimeout = $timeout(function () { // arbitrary code }, 55); $timeout. cancel(customTimeout); The same applies to “$interval()”. To disable a watch, just call it.


2 Answers

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

like image 63
Rasalom Avatar answered Oct 05 '22 19:10

Rasalom


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) {
...
}]);
like image 34
kubuntu Avatar answered Oct 05 '22 19:10

kubuntu