Naughty.Coder
Naughty.Coder

Reputation: 3960

Updating scope attributes binded with promises

The name property of the scope is not updated with the value returned from the $timeout.
$timeout returns a promise resolved by the value returned from the function provided to it. And I read the view bindings accept promises. But it doesn't work for me.

      angular.module('myApp',[]).controller('ctrl',function($scope,$timeout){


          $scope.name = $timeout(function () {
              return "World";
          }, 3000);
      })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="ctrl">
<h1>Hello, {{name}}!</h1>

</body>

Upvotes: 0

Views: 40

Answers (2)

Josep
Josep

Reputation: 13071

The way that you are using the $timeout function is wrong, do this instead:

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

The $timeout function retrieves a promise, not the return value of your inner function.

Upvotes: 2

zs2020
zs2020

Reputation: 54542

Change it to

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

Upvotes: 3

Related Questions