Javascript 使用php从angular js中的数据库获取数据
我是angular js新手,我想使用php从数据库中获取数据。我尝试下面的代码Javascript 使用php从angular js中的数据库获取数据,javascript,php,angularjs,ajax,Javascript,Php,Angularjs,Ajax,我是angular js新手,我想使用php从数据库中获取数据。我尝试下面的代码 <html> <head> <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> <script> var myApp = angular.mod
<html>
<head>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp.controller('studentController', ['$scope','$http', function ($scope,$http) {
var url = "ajax.php";
$http.get(url).success( function(response) {
$scope.students = response;
});
}]);
</script>
</head>
<body>
<div ng-app = "myApp" ng-controller = "studentController">
<table>
<tr>
<th>Name</th>
</tr>
<tr ng-repeat = "student in students">
<td>{{ student.Name }}</td>
</tr>
</table>
</div>
</body>
</html>
但我不能在前端显示…谢谢
<?php
$con = mysqli_connect("localhost","root","","adworld");
$res= mysqli_query($con,"SELECT * FROM students");
$result = array();
while($row=mysqli_fetch_assoc($res)){
$result[] = $row;
}
echo json_encode($result);
?>
工作代码:
<!DOCTYPE html>
<html >
<script src= "angular.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr>
<th>Name</th>
<th>Roll</th>
</tr>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Roll}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("ajax.php")
.success(function (response) {$scope.names = response;});
});
</script>
</body>
</html>
名称
滚
{{x.Name}
{{x.Roll}}
var-app=angular.module('myApp',[]);
app.controller('customersCtrl',函数($scope,$http){
$http.get(“ajax.php”)
.success(函数(响应){$scope.names=response;});
});
ajax.php
<?php
$conn = new mysqli("localhost","root","","adworld");
$result = $conn->query("SELECT * FROM students");
$arr = array();
while($rs = $result->fetch_assoc())
{
$arr[] = $rs;
}
echo json_encode($arr);
?>
工作代码:
<!DOCTYPE html>
<html >
<script src= "angular.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr>
<th>Name</th>
<th>Roll</th>
</tr>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Roll}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("ajax.php")
.success(function (response) {$scope.names = response;});
});
</script>
</body>
</html>
名称
滚
{{x.Name}
{{x.Roll}}
var-app=angular.module('myApp',[]);
app.controller('customersCtrl',函数($scope,$http){
$http.get(“ajax.php”)
.success(函数(响应){$scope.names=response;});
});
ajax.php
<?php
$conn = new mysqli("localhost","root","","adworld");
$result = $conn->query("SELECT * FROM students");
$arr = array();
while($rs = $result->fetch_assoc())
{
$arr[] = $rs;
}
echo json_encode($arr);
?>
您需要在应用程序中注册控制器-myApp.controller(“studentController”['$scope',function($scope){$scope.greeting='Hola!';}])代码>@messerbill:不工作…请更新您的代码好吗?@messerbill:代码更新…您需要在应用程序中注册控制器-myApp.controller(“studentController”,['$scope',function($scope){$scope.greeting='Hola!';})代码>@messerbill:不工作…请更新您的代码好吗?@messerbill:代码更新。。。。