Reputation: 65
I'm developing a real time web application, I'm using angular as javascript framework and in the server side I'm using play framework, to make the real time app, I'm using the comet technic, where basically I push scripts from the server.
I'm actually tryng to communicate from the script that push from the server to the angular controller, and I'm a little stuck.
Here is what I've tried.
My View:
<div id="workStation" ng-controller="workStation">
....
<input type="text" id="bridge" ng-change="serverEvent()" ng-model="bridge">
<iframe hidden="hidden" id="messages" src="/register"></iframe>
</div>
My Controller
var app = angular.module('debQApp',[]);
app.controller('workStation'['$scope','$timeout','$interval','api','websocket','$http','$q',function($scope,$timeout,$interval,api,websocket,$http,$q)
{
....
$scope.bridge = 'waiting';
$scope.serverEvent = function()
{
console.log('Event');
};
}
The problem is that the function serverEvent is never call. The script I push from the server only modify the input.
<script>parent.document.getElementById('bridge').value = 'event';</script>
Actually the script do its job because I see the input changing its value, but I can't make angular notice the change.
Use a different technic as websocket is not a possibility because of the client needs.
Note: In the controller I do a lot of things that are not relevant to the post, for this reason I have omitted, I really need angular notice the change.
Thanks.
Upvotes: 1
Views: 1214
Reputation: 180
You can get the scope of a controller with something like this:
<script>
var cometListener = function(event){
angular.element($('#workStation')).scope().serverEvent(event);
}
</script>
One more thing, you should note is that scopes are initialized after the page is loaded, so calling methods from outside of scope should always be done after the page is loaded. Else you will not get to the scope at all.
Upvotes: 1