S
S
Sergey Nikolaev2014-12-27 14:49:10
PHP
Sergey Nikolaev, 2014-12-27 14:49:10

How to update data in angular.js from php server?

Good afternoon, how to update data when developing on angular.js?

// контроллер, который должен обновляться, 
// каждые n секунд, когда открыта страница с этим контроллером
.controller('AdminCtrl', function($scope, Factory) {
                 Factory.getReserv(function (results) {
                    $scope.resers = results;
                })
    })
//фактори
.factory('Factory', function($http) {
        return {
            getReserv: function(callback){
                var url = 'url запроса';
                $http.post(url).success(callback);
            } 
})

I tried many options:
through SetInterval (the problem is that with each new opening, the timer starts again and again and again) I
tried to solve it by writing to the object and checking for existence, it doesn’t work if declared through a global variable, the output stops working ...
Shouldn't there be a reasonable option in Angular for updating data on a page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsvladimir, 2014-12-27
@vsvladimir

Instead of setInterval , it's better to use the $interval service. And then you have to stop the timer. $scope.refresh - a function for updating the content of the page - you can replace it with your own.

var stop = $interval($scope.refresh, 1000);
$scope.$on('$destroy', function() {
    $interval.cancel(stop);
});

In this example, these functions are placed in the controller.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question