2

Here i add a delay in javascript forloop using $timeout. Unexpectedly i got an error saying
ReferenceError $timeout is not defined. i am new to angularjs please help me. PLNKR


function CompLibrary() {
  return {
    init: init
  }
  function init(dependencies, controller) {
    dependencies.push(controller);
    angularApp.controller('MainCtrl', dependencies);
  }
}
var compX = CompLibrary();
compX.init(deps, _controller);
function _controller() {
  var ViewModel = this;
  ViewModel.search = "Name";
  ViewModel.quantity = 1;

  for(var i = 0; i < 4; i++) {
    (function(i){ 
        $timeout(function() {
            ViewModel.quantity++;
        }, i * 2000);
    })(i); // Pass in i here
  }

}

2 Answers 2

9

You have to inject the $timeout into the controller function.

function _controller($timeout) { ... }

Please see updated Plunkr

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

Comments

-2
var deps = [];
var angularApp = angular.module('plunker',[]);
function CompLibrary() {
  return {
    init: init
  }
  function init(dependencies, controller) {
    dependencies.push('$timeout');
    dependencies.push(controller);
    angularApp.controller('MainCtrl', dependencies);
  }
}
var compX = CompLibrary();
compX.init(deps, _controller);
function _controller($timeout) {
  var ViewModel = this;
  ViewModel.search = "Name";
  ViewModel.quantity = 1;

  for(var i = 0; i < 4; i++) {
    (function(i){ 
        $timeout(function() {
            ViewModel.quantity++;
        }, i * 2000);
    })(i); // Pass in i here
  }

}

By injecting $timeout in the controller function we can solve this problem.

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.