如何使用angularjs使您的站点更加生动?
你好,我是stackoverflow的新手, 我试图用HTML、CSS、PHP和angularjs构建一个聊天系统 但我在中间堆积 我的问题是为什么我的网站没有使用angularjs进行实时更新 我试图从SQL server获取一个实时选择查询,当我试图更改值以更新行时,只有在刷新站点时,才会在站点中发生更改记录的情况 也许我对angularjs了解不够 我应该如何解决这个问题 这是我的脚本:index.html如何使用angularjs使您的站点更加生动?,angularjs,Angularjs,你好,我是stackoverflow的新手, 我试图用HTML、CSS、PHP和angularjs构建一个聊天系统 但我在中间堆积 我的问题是为什么我的网站没有使用angularjs进行实时更新 我试图从SQL server获取一个实时选择查询,当我试图更改值以更新行时,只有在刷新站点时,才会在站点中发生更改记录的情况 也许我对angularjs了解不够 我应该如何解决这个问题 这是我的脚本:index.html <html> <style> table, th, td
<html>
<style>
table, th, td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Message }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("demo_index.php")
.then(function (response) {$scope.names = response.data.records;});
});
</script>
</body>
</html>
demo_index.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Connection details [SQL]
$servername = "localhost";
$username = "****************";
$password = "***************";
$dbname="**************";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * From chat" ;
$outp = "";
$result = $conn->query($sql);
$num_rows = mysqli_num_rows($result);
if($num_rows>=0)
{
while($row = $result->fetch_assoc()){
if ($outp != "") {$outp .= ",";}
$outp .= '{"Name":"' . $row["name"] . '",';
$outp .= '"id":"' . $row["id"] . '",';
$outp .= '"Message":"'. $row["message"] . '"}';
}
$outp ='{"records":['.$outp.']}';
}
$conn->close();
echo($outp);
?>
app.controller'customersCtrl',函数$scope,$http{$scope.names=[];$http.getdemo_index.php。然后函数响应{$scope.names=response.data.records;};};并使用json_encode$arr/json_decode$str进行json编码/解码。我正在使用json代码和您的控制器,但它不起作用。嗯。。。您是否只想更改SQL数据库中的值?在不重新加载的情况下,应该在站点上更新哪些内容?如果是-当:app.controller'customersCtrl',函数$scope,$http,$timeout{$timeout$scope.refreshData,200;$scope.refreshData=函数{}$http.getdemo_index.php。然后函数响应{$scope.names=response.data.records;};$scope.refreshData;};但请阅读有关套接字的内容,因为这是一种更好的方法。Socket.io将帮助您app.controller'customersCtrl',函数$scope,$http{$scope.names=[];$http.getdemo_index.php。然后函数响应{$scope.names=response.data.records;};};并使用json_encode$arr/json_decode$str进行json编码/解码。我正在使用json代码和您的控制器,但它不起作用。嗯。。。您是否只想更改SQL数据库中的值?在不重新加载的情况下,应该在站点上更新哪些内容?如果是-当:app.controller'customersCtrl',函数$scope,$http,$timeout{$timeout$scope.refreshData,200;$scope.refreshData=函数{}$http.getdemo_index.php。然后函数响应{$scope.names=response.data.records;};$scope.refreshData;};但请阅读有关套接字的内容,因为这是一种更好的方法。Socket.io将帮助您