var app = angular.module('plunker', ['rx']);
app.controller('MainCtrl', function($scope, rx) {
$scope.counter;
$scope.buttonEnabled = true;
var observable = rx.Observable
.interval(1000)
.take(3)
.safeApply($scope, function(x) {
$scope.counter = x;
});
$scope.click = function(){
// angular.noop is just a shorthand for an empty function function(){}
observable.subscribe(
function onNext(value){
console.log(value);
}, // onNext
angular.noop, // onError
function onCompleted(){
console.log('Completed');
$scope.$apply(function(){
$scope.buttonEnabled = false;
})
});
//forEach is an alias for subscribe
// o.forEach(function(){});
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js" data-semver="1.4.5"></script>
<script data-require="rxjs@*" data-semver="2.5.3" src="//cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.3/rx.all.js"></script>
<script data-require="rx.angular.js@*" data-semver="0.0.14" src="https://cdn.rawgit.com/Reactive-Extensions/rx.angular.js/v0.0.14/dist/rx.angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<button ng-click="click()" ng-disabled="!buttonEnabled">Start</button>
<span>{{counter}}</span>
</body>
</html>
body {
font-size: 10em;
}