gpt4 book ai didi

php - 使用 Angular 从 mysql 自动重新加载表

转载 作者:行者123 更新时间:2023-11-29 18:19:09 25 4
gpt4 key购买 nike

我正在使用 http.get(url) 从我的 mysql 数据库中调用信息,最后用该信息填充一个表。

这是可行的,但现在我希望 Controller 在数据库发生更改时自动调用 http.get(url),而无需手动刷新页面。

评论或提示很受欢迎:D(我对 Angular 有点陌生)。

这是我当前的代码:

数据.js

angular.module('demo', [])
.controller('Hello', function($scope, $http, $timeout) {

$scope.loadData = function(){

$http.get('http://localhost:8888/nuevo/respuesta.php').
then(function(response) {
$scope.greeting = response.data;
//console.log($scope.greeting);
//console.log("hola");
});

};//se cierra la funcion

$scope.loadData();
});

index.html

<div ng-controller="Hello">

<table>
<tr>
<th>ID</th>
<th>Banda</th>
<th>Cancion</th>
</tr>
<tr ng-repeat="x in greeting">
<td>{{x.`enter code here`IdCancion}}</td>
<td>{{x.Bandas_Musicales}}</td>
<td>{{x.Canciones}}</td>
</tr>
</table>
<button ng-click="loadData()">reload</button>
</div>

最佳答案

为了通知客户端服务器上的数据更改,您需要找到一种让服务器联系客户端的方法。一个流行的解决方案是 websockets,也许可以研究一下。另外,一些 php 框架已经为此做好了准备。
如果您不想进入 websockets,您可以考虑每 x 秒刷新一次表数据,以使其保持最新。

关于php - 使用 Angular 从 mysql 自动重新加载表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com