Angular.js每秒调用$ http.get

问题描述 投票:0回答:1

如何每秒调用$ http.get来更新我的页面?

var app = angular.module("CompanionApp", []);

app.controller('LoginController', function ($scope, $http) {
    $scope.LoginSubmit = function() {
        $http.get('/api/player/' + $scope.name)
        .then(function(res) {
            $scope.connected = res.data.connected;
            $scope.health = res.data.health;
            $scope.armour = res.data.armour;
        })
    };
});
angularjs http get seconds method-call
1个回答
1
投票

试试$interval

var app = angular.module("CompanionApp", []);

app.controller('LoginController', function ($scope, $http, $interval) {
    var interval;
    $scope.LoginSubmit = function() {
      interval = $interval(function () {
        $http.get('/api/player/' + $scope.name)
        .then(function(res) {
            $scope.connected = res.data.connected;
            $scope.health = res.data.health;
            $scope.armour = res.data.armour;
        })
       }, 1000);
    };

    $scope.stopCalls = function(){ // incase you want to stop the calls using some button click
      $interval.cancel(interval);
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.