1

I have a button for which I have set ng-click="refresh()". The console logs before the timeout function logs correctly but the console log inside the $timeout block and after the block doesn't seem to log. If I remove the $timeout block every console log works. I even checked with $interval instead of $timeout but same behaviour.

I wanted to do something like this here

I'm using Angular.js 1.4.0

This is my implementation inside the controller

      $scope.refreshing ={state: false};
      $scope.refresh = function(){
        console.log($scope);
        $scope.refreshing.state = true;
        $scope.search(); //sends an http request and loads results.
        console.log('this logs');
        // $scope.refreshing.state = false;
        $timeout(function(){
           console.log('this doesnt log')
           $scope.refreshing.state = false;
         },2000);
        console.log('this doesnt log')

      }
cmgchess
  • 7,996
  • 37
  • 44
  • 62
  • Sounds like it's crashing. Any errors in the console? Maybe `$timeout` isn't defined. – Jacob Stamm Feb 08 '22 at 14:04
  • Yes. You can find all the built-in AngularJS services listed [here](https://docs.angularjs.org/api/ng/service). Can you edit your question to include the controller function definition, as well as the code where you're registering the controller in your angular.module()? And to be clear, you're saying there are no errors in your JS console? – Jacob Stamm Feb 08 '22 at 14:11
  • You're not answering my question about the JS console errors – Jacob Stamm Feb 08 '22 at 14:21
  • I need to see the definition of your controller function to make sure you're using the dependency injection properly – Jacob Stamm Feb 08 '22 at 14:24
  • 1
    or great now it makes sense. i havent added the $timeout in the dependancy array – cmgchess Feb 08 '22 at 14:26
  • That makes sense. As to why you're not getting `Uncaught ReferenceError: $timeout is not defined` in your JS console, I have no clue. Are you catching the exception and not logging it by any chance? – Jacob Stamm Feb 08 '22 at 14:28
  • im also not doing anything as such also vscode didnt even warn or anything. im new to ang1 so i miss these things – cmgchess Feb 08 '22 at 14:30
  • No problem. Your IDE wouldn't be able to warn you of this kind of issue because it has no way of knowing about the dependency injection behind the scenes and whether or not a function parameter like `$timeout` has been defined at runtime. My apologies if this is already something you know, but the dev tools in your browser is how you'll want to troubleshoot these sorts of issues. – Jacob Stamm Feb 08 '22 at 14:33

1 Answers1

1

You'll want to make sure your dependency injection is wired up properly. For example:

angular
    .module("MyApp")
    .controller("MyController", ["$scope", "$timeout", MyController]);

function MyController($scope, $timeout) {
    // controller code
}

See John Papa's AngularJS style guide for some best practices relating to controllers.

Jacob Stamm
  • 1,660
  • 1
  • 29
  • 53