Angular: Empty Fiddle

http://angularjs.org/

by dave ho

HTML

<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
    <p>Iteration: {{ curTry }}</p>
    <p>New Rnd: {{ rnd }}</p>
    
    <h3>New Total: {{ sum }}</h3>
    
    <h1>{{ msg }}</h1>
</div>

JavaScript

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

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope, $q) {
    //$scope.name = 'Superhero';
    var tries = 10;
    $scope.msg = 'Your Balls';
    $scope.curTry = 0;
    $scope.rnd = 0;
    $scope.sum = 0;
    
    
  var wait = function() {
    var deferred = $q.defer();
    setTimeout(function() {
       // alert('1000');
       $scope.rnd = gen();
       $scope.curTry++;
      // Reject 3 out of 10 times to simulate 
      // some business logic.
        if ($scope.rnd > 10.7) { 
           deferred.reject(0);
        } else { 
            deferred.resolve($scope.rnd);
        }
    }, 1000);
    // alert('0');
    return deferred.promise;
  };
    
    var gen = function() {
        return Math.random();    
    };
    
    wait().then(function(i) {
        alert(i);
        $scope.$apply(function() {
           $scope.msg = Math.random();
           $scope.curTry++;
           $scope.sum += i;
       });   
    });
}