Clock, v1.1.5

Base for single controller examples

by Zaiste

HTML

<link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/4.3.1/css/foundation.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
<div>{{clock}}</div>

<div ng-controller="Ctrl2">{{clock}}</div>

JavaScript

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

app.controller('Ctrl', function ($scope, $location, $timeout) {
    // At the moment there is no setInterval alternative in Angular
    // The only way to simulate it is a $timeout function which 
    // calls itself recursively
    var timer = $timeout(function updateClock() {
        $scope.clock = new Date();

        timer = $timeout(updateClock, 1000);
    }, 1000);

    // once view is destroyed, you can destroy it with 
    // listening on $destroy:
    $scope.$on('$destroy', function (e) {
        $timeout.cancel(cancelRefresh);
    });
});

app.controller('Ctrl2', function ($scope, $location) {
    // with $apply
    setInterval(function(){
        $scope.$apply(function() {
            $scope.clock = "Ctrl2: " + new Date();
        });
    }, 1000);
});