I'm trying to convert an application from pure JS to AngularJS and have run into a problem that I've extracted into the following code snipet.
<!DOCTYPE html>
<html ng-app="testApp">
<body>
<div ng-controller="eventACtrl">
<div>{{day}}</div>
<lable for="filteredName">Filter:</label>
<input type="text" name="filteredName" ng-model="filteredName"/>
<table>
<tbody>
<tr ng-repeat="module in modules | matchFilter:filteredName | orderBy: 'name'">
<td>{{$index+1}}</td>
<td>{{module.name}}</td>
</tr>
</tbody>
</table>
</div>
<div ng-controller="eventBCtrl">
{{cpu}}
</div>
<script src="js/angular.min.js"></script>
<script src="js/test.js"></script>
</body>
</html>
var testApp = angular.module("testApp", []);
testApp.controller("eventACtrl", function($scope) {
var eventACallback = function(e) { $scope.$apply(function() {
var pData = JSON.parse(e.data);
var sDate = new Date(Number(pData.date));
$scope.day = sDate.toDateString() + " " + sDate.toLocaleTimeString();
$scope.modules = pData.modules;
console.log("EVENTA");
});
}
var source = new EventSource("http://" + location.host +"/EVENTS:A");
source.addEventListener("EVENTA", eventACallback, false);
});
testApp.controller("eventBCtrl", function($scope) {
var eventBCallback = function(e) {
$scope.$apply(function() {
var pData = JSON.parse(e.data);
$scope.cpu = pData.cpu;
console.log("EVENTB");
});
}
var source = new EventSource("http://" + location.host + "/EVENTS:B");
source.addEventListener("EVENTB", eventBCallback, false);
});
testApp.filter("matchFilter", function() {
return function(modules, filteredName) {
console.log("filter: " + filteredName);
var newModules = [];
for (var i in modules) {
if (modules[i].name.search(filteredName) != -1) {
newModules.push(modules[i]);
} else
continue;
}
return newModules;
};
});
var eventACallback = function(e) { var pData = JSON.parse(e.data);
var sDate = new Date(Number(pData.date)); $scope.cpu = pData.cpu;
$scope.day = sDate.toDateString() + " " + sDate.toLocaleTimeString();
$scope.modules = pData.modules;
console.log("EVENTA");
};Hi,
You are right. This is because of $scope.$apply That’s the reason you should use it as sparingly as possible. an $apply will make sure that every watch in your application is fired at least once. The reason is, that in your eventBCallback you might have changed something that otherwise might get unnoticed, (lets assume you add a new module to the modules array (yeah I know that isn't easy possible,but angular does not!)) and therefore the filters need too be fired also.
If you are really sure that what you are doing in your evenBCallback does not affect other places, but indeed only the local scope, you can sue $scope.$digest() in stead of $scope.$apply, but you better be sure you know what you are doing!
Regards
Sander
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/FCyO0IMU-g0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
--
--
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Thanks. But I was under the assumption that each controller will have a separate scope. I'm not using nested controllers.
If this is a valid assumption, then why should modifying the scope of one controller effect the other?