gpt4 book ai didi

javascript - $http.get AngularJS 不返回数据

转载 作者:行者123 更新时间:2023-11-30 16:34:55 25 4
gpt4 key购买 nike

我正在学习 AngularJS,但我无法让我的 mysql 数据库中的数据显示在我的 View 中。我的代码:

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);
});
}

todo-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>

todo-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);

当我检查我的控制台时,它返回对象 enter image description here

但是我的数据库中的数据没有显示在 View 中。我的连接是正确的。我在这里错过了什么吗?谢谢。

最佳答案

正确的语法应该是-

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);
});
}

注意 - 成功函数采用响应(在您的案例中为数据)对象。返回的实际值应该是 response.data(在你的例子中是 data.data)。

关于javascript - $http.get AngularJS 不返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32837016/

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