I have a current-time directive using $timeout, and it appears to be invoking functions in a separate controller. I'm confused why this is, I thought Controllers contained their own scope and were isolated from each other?,
Basically, myFunction is being invoked when the currentTime directive $timeout occurs. Is it a big deal? Will this cause troubles for me down the road? Can someone help me understand this behavior? Here is a plunker that demonstrates the behavior I'm talking about.
Here is my HTML:
<html ng-app="myapp">
<head> <!-- using Angular version 1.2.0-rc3 --> </head>
<body>
<div ng-controller="TimeController">
<span current-time></span>
</div>
<div ng-controller="MyController">
<span class="grrr">{{myFunction()}}</span>
</div>
</body>
</html>
My Angular code:
var myapp = angular.module('myapp', []);
// just to show that I'm using my currentTime directive in separate controller.
// but it still happens if remove TimeController.
myapp.controller('TimeController', ['$scope', function($scope) {
console.log('entering TimeController');
}]);
myapp.controller('MyController', ['$scope', function($scope) {
$scope.myFunction = function() {
console.log('calling myFunction');
return 'myFunction was last called @ ' + new Date().toString();
};
}]);
// continuous update for time
myapp.directive('currentTime', ['$timeout', function($timeout) {
return function(scope,element,attrs) {
function updateTime() {
$timeout(function() {
element.text(new Date().toString());
updateTime();
}, 1000);
};
console.log('start clock');
updateTime();
};
}]);