AngularJS data in one step

I track the map coordinates using angularJS to update the data, however I ran into an odd problem when the data you see on the screen does not match the operator console.

zombie.controller("move", function($scope) {
    io.on("location", function(data) {
        console.log(data);
        $scope.location = data.loc;
    })
    $scope.move = function(direction) {
        $scope.title = ": Traveling";
        io.emit("move", {direction:direction});
    }
});

The console will write something like: Object {loc: "(59,30)"}

Suppose the previous data was Object {loc: "(60,31)"}. My page will print (60,31)when the console registers (59,30).

In addition, when the page loads, the initial click does not display anything, but the console registers the correct data.

I tried to move io.on('location')inside the angular function, but if it is inside move(), it will go to breakers and is written as 15 times in a row and lags. Outside functions are fine, except for this problem. Any thoughts?

+3
2

io.on("location") socket.io Angular , . , . $scope.$apply(), ...

io.on("location", function(data) {
    console.log(data);
    $scope.location = data.loc;
    $scope.$apply();
})
+6

, , , ,

, .

setTimeout(function(){
    $scope.apply();
}, 50);

.

0

Source: https://habr.com/ru/post/1679052/


All Articles