我正在学习AngularJS,而我无法获取我的mysql数据库中的数据以显示在我的视图中。我的代码:
todoController.js
angular
.module('todoApp')
.controller('todoController', todoController)
.config(config);
function config($routeProvider) {
$routeProvider
.when('/', {
controller: 'todoController',
templateUrl: 'app/views/todo-list.html'
});
}
function todoController($http, $scope) {
$http.get('app/endpoints/todo-list.php')
.then(function successCallback(data) {
$scope.todos = data;
console.log(data);
}, function errorCallback(data) {
console.log(data);
});
}
待办事项-list.html
<table cellpadding="4">
<tr ng-repeat="t in todos">
<td>{{ t.name }}</td>
<td>Remove</td>
</tr>
</table>
的index.html
<!DOCTYPE html>
<html lang="en" ng-app="todoApp">
<head>
<meta charset="UTF-8">
<title>AngularJS</title>
</head>
<body>
<div ng-view><!-- data will be loaded here--></div>
<script type="text/javascript" src="assets/jquery/jquery.js"></script>
<script type="text/javascript" src="assets/angular/angular.js"></script>
<script type="text/javascript" src="assets/angular/angular-route.js"></script>
<script type="text/javascript" src="app.js"></script>
<!--controllers-->
<script type="text/javascript" src="app/controllers/todoController.js"></script>
</body>
</html>
待办事项-list.php的
<?php
require '../../connection.php';
$statement = $db->prepare("SELECT * FROM todo_list");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($results);
但是我的数据库中的数据并没有显示在视图中。我的联系是正确的。我在这里错过了什么吗?谢谢。
答案 0 :(得分:2)
正确的语法是 -
function todoController($http, $scope) {
$http.get('app/endpoints/todo-list.php')
.then(function successCallback(response) {
$scope.todos = response.data;
console.log(response);
}, function errorCallback(reason) {
console.log(reason);
});
}
注意 - success函数接受响应(在您的情况下为数据)对象。返回的实际值应该是response.data(在您的情况下为data.data)。