我检查过几个帖子,但我无法使其发挥作用。到目前为止我来到这里:
myApp.controller('customersCtrl', function($scope, $http, $timeout) {
var polling = function() {
var value = $http({
method: 'GET',
url: 'poll.php'
});
value.success(function(data, status, headers, config) {
$scope.records = data;
});
$timeout(function() {
polling();
}, 3000);
};
polling();
});

<?php
$db = new mysqli('fdb4.freehostingeu.com', '1584066_users', '*******', '1584066_users');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$db->set_charset("utf8");
if(!$result = $db->query("SELECT * FROM mtp ORDER BY date DESC")){
die('There was an error running the query [' . $db->error . ']');
}
while($row = mysqli_fetch_assoc($result)){
$row["date"] = strtotime($row["date"]) * 1000;
$records[] = $row;
}
print( json_encode($records));
$result->free();
$db->close();
?>
它适用于第一个poll();
调用,但如果从外部更新SQL表,则不会刷新。
编辑:我偶然尝试过,不知怎的,POST方法适用于我的情况。
答案 0 :(得分:0)
当你使用$ interval或$ timeout时,有时你需要使用$ apply,尝试使用$ apply,它会起作用。
以下是您的回答
myApp.controller('customersCtrl', function($scope, $http, $timeout) {
var polling = function() {
var value = $http({
method: 'GET',
url: 'poll.php'
});
value.success(function(data, status, headers, config) {
$scope.records = data;
$scope.$apply(); //Use $apply to start digest process manually.
$timeout(function() {
polling();
}, 3000);
});
};
});
&#13;
答案 1 :(得分:-1)
您需要$interval
代替$timeout
。
$ timeout只等待3秒然后拨打电话然后停止。使用$ interval,您可以等待3秒,调用轮询,等待3秒,调用轮询等...
myApp.controller('customersCtrl', function($scope, $http, $interval) {
var polling = function() {
var value = $http({
method: 'GET',
url: 'poll.php'
});
value.success(function(data, status, headers, config) {
$scope.records = data;
});
$interval(function() {
polling();
}, 3000);
};
polling();
});