的index.phpAngularJS链接到数据库
<!doctype html>
<html ng-app="rjtApp">
<head>
<title>PHP MySQL API Consumed with AngularJS</title>
<script src="angular.min.js"></script>
<script src="data.js"></script>
</head>
<body>
<div ng-controller="GetUsers">
<table>
<thead><tr><th>Name</th></tr></thead>
<tbody>
<tr ng-repeat="user in users"><td>{{ user.name }}</td></tr>
</tbody>
</tfoot></tfoot>
</table>
</div>
</body>
</html>
api.php(这方面已经没有问题,我测试。)
<?php
// set up the connection variables
$db_name = 'air';
$hostname = '127.0.0.1';
$username = 'root';
$password = '123456';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
// a query get all the records from the users table
$sql = 'SELECT USER_NAME FROM air_users LIMIT 1';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare($sql);
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// convert to json
$json = json_encode($result);
// echo the json string
echo $json;
?>
data.js
alert("this is connect");
var app = angular.module("rjtApp", []);
app.controller("GetUsers", function($scope, $http)
{
function getProject(){
$http.get("api.php").success(function(data){
$scope.projects = data; //the data are stored in projects
});
};
});
getProject();
我的本意做一个现场验证,以检查数据库名退出,但我甚至不知道如何将AngularJS连接到数据库,我一直在做错什么?
我希望但仍然没有运气,并欢迎堆栈溢出,这里的一切都很棒。 – user3233074
感谢user3233074!我再次查看了您的代码并注意到了一些事情。首先,在JavaScript中调用控制器GetUser,在HTML中调用GetUser。所以你只需要匹配名称就可以使用控制器本身。其次,你需要关闭控制器(你错过了'});'最后)。在这两个更改之后,加载页面时会弹出放置在getProject()中的警报。我会进行这些更改并查看是否从数据库获取数据。让我知道它是如何工作的。噢,如果有帮助,这里是我使用的撬棍:http://jsfiddle.net/dakra/U3pVM/。 –
我改变了你告诉我的,但我仍然没有从数据库中获取数据。 – user3233074