Angularjs - then() needs digest - 14657680

http://angularjs.org/

by mrajcok

HTML

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js"></script>
<p style="padding-bottom:20px">
If you click the button before the timer resolves the promise (2 seconds) then name3 gets populated.  If you click the button after the timer resolves, then nothing is displayed for name3.  Unless you add an $apply call in the then function.
</p>
<div ng-controller="MyCtrl">
    Hello, {{name}}! <br/>
    {{name2}}<br/>
    <button ng-click="go()">Clickme</button><br/>
    <button ng-click="">Force digest by clicking me</button><br/>
    {{name3}}
</div>

JavaScript

var myApp = angular.module('myApp',[]);

function MyCtrl($scope, $q, $timeout) {
    var data = $q.defer();    
    $timeout(function() { data.resolve("Some Data") }, 2000);
    var p = data.promise;

    $scope.name = p.then(angular.uppercase);
    $scope.name2 = p.then(function(x) { return "Hi "+x;});
    //$scope.name2 = "Hi " + p;
    $scope.go = function() {
            $scope.name3 = p.then(function(x) { 
                // uncomment this to make it work:
                //$scope.$apply();
                return "Finally: "+x;
            });
    };
 }