javascript - Angular.js- Call scope function from outside the controller? -
i new angular.js , trying figure out how pass json object handler scope function in controller(or service instead?). want event listener outside of controllers, etc. because convention of on event handler listen events, rather several event listeners each event. how can angular.js?
client code:
var source = new eventsource('/subscribe'); source.addeventlistener('open', function(e) { console.log("connection opened"); }); //not using multi-listeners each event. source.addeventlistener('message', function(e) { var result = json.parse(e.data); event = object.keys(result)[0]; if (event == "cpuresult") { //call function of cpuscope //pass json } else if if (event == "networkresult") { //call function of neworkscope , on.... }); source.addeventlistener('error', function(e) { if (e.readystate == eventsource.closed) { } }, false);
rough angular.js example:
function cpuctrl($scope) { // last received msg $scope.msg = {}; var updateelement = function (msg) { $scope.$apply(function () { $scope.msg = json.parse(msg.data) }); } }
Comments
Post a Comment