I'm trying to change my angular model after a websocket push from the server. How is it possible to change values like $scope.contacts
each time the server serves new data..?
I'm not sure if it is possible by using $apply
. I know that I can access the DOM element retrieve the scope and then change the values, but there should be a better solution!
I'm really interested in a solution to update the angular model from outside without creating angular modules since I'm using relative data sources that emit change events. Is there no simple way to do that like in Backbone.js where you can say:
var book = new Backbone.Model({ title: 'value' });
book.set("title", "A Scandal in Bohemia");
What I want in angularjs is something like:
function MyController($scope) {
$scope.contacts = [];
}
datasource changed -> function () {
MyController.set('contacts', 'value'); // change angular scope property
}
just do it like below
Look at socket.io angular service:
and controller using it:
Well, the marked answer is certainly not a "simple way" as requested by OP. Here's a much simpler solution.
Retrieve the scope wherever you want in your own JavaScript code:
Change a value in your Angular
$scope
variable, but from your external JavaScript:Here's a JSFiddle.