Angularjs chaining

Angularjs chaining

by cmyworld

HTML

<div ng-app="myApp">
    <div ng-controller="myController">
        <button ng-click="testPromise()">Test Promise</button>
        <div>Promise message: {{promiseMessage}}</div>
    </div>
</div>

JavaScript

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

app.controller('myController',['$scope','$timeout',function($scope,$timeout) {
   $scope.message='Hello Angular';
    $scope.promiseMessage="Initial Message";
    $scope.testPromise=function() {
        var twoSecTimeout= $timeout(function(){
            $scope.promiseMessage="After 2 seconds";
        },2000);
        twoSecTimeout.then(function(data) {
            $scope.promiseMessage="Now in then after 2 seconds";
        });
    }
}]);