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')

      }
8
  • Sounds like it's crashing. Any errors in the console? Maybe $timeout isn't defined. Commented Feb 8, 2022 at 14:04
  • Yes. You can find all the built-in AngularJS services listed here. 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? Commented Feb 8, 2022 at 14:11
  • You're not answering my question about the JS console errors Commented Feb 8, 2022 at 14:21
  • I need to see the definition of your controller function to make sure you're using the dependency injection properly Commented Feb 8, 2022 at 14:24
  • 1
    or great now it makes sense. i havent added the $timeout in the dependancy array Commented Feb 8, 2022 at 14:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.