1

I was reading Mastering Web Application Development with AngularJS book, and in the chapter 3 when the autors took about $q and promises they write and example of a single $timeout.

index.html

<h1>Hello, {{name}}!</h1>

controller.js

$scope.name = $timeout(function () {
  return "World";
}, 2000);

The thing is that i test the code and this don't work for me, i don't if i write some wrong, i write it and look it several time but i don't know where is the error.

I change the code in the controller for:

$timeout(function () {
     $scope.name = "World";
}, 2000);

and work perfectly.

Anyone know why this happened?, I include a plunk of the example here

1 Answer 1

2

It is because timeout returns promise, not a string. return inside callback returns what will be passed along the chain.

Correct way of using it would be:

$timeout(function () {
          return "World";
    }, 2000).then(function(p) {
      $scope.name = p;
    });
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.