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.


I have two controllers that each call an SSE server and each has its own 
callback function. My understanding is that $scope for each controller 
would be different and modifying one would not effect the other.

However, whenever eventBCallBack() in eventBCtrl is executed, it seems to 
effect $scope of eventACtrl. My filter which is being called in eventACtrl 
is executed whenever eventBCallBack() is executed. Even if eventBCallBack() 
is an empty function, it makes no difference.

I suspect it has something to do with $scope.$apply.

Following is the HTML file:

    <!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>



Following is the javascript code:

    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;
      };
    });



-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to