gpt4 book ai didi

javascript - 在 AngularJS 中使用工厂显示数据

转载 作者:行者123 更新时间:2023-12-02 16:55:13 27 4
gpt4 key购买 nike

我想将数据库中的数据显示到页面上。我正在使用工厂服务,并且我在 firebug 中看到 json 。
这是我的代码:

HTML

<div ng-controller="fruitsController">
<ul>
<li ng-repeat="fruit in fruits">
{{fruit.subject}}
</li>
</ul>
</div>

JS

var fruitsApp = angular.module('fruitsApp', [])

fruitsApp.factory('fruitsFactory', function($http) {
return {
getFruitsAsync: function(callback) {
$http.get('insert.php').then(
function(response){
var store = [];
store = response.data;
},
function(error){
console.log(error);
});

}
};
});

fruitsApp.controller('fruitsController', function($scope, fruitsFactory) {
fruitsFactory.getFruitsAsync(function(results) {
console.log('fruitsController async returned value');
$scope.fruits = results.fruits;
});
});

这是insert.php

include('config.php');


$data = json_decode(file_get_contents("php://input"));
$subject = mysql_real_escape_string($data->subject);
$body = mysql_real_escape_string($data->body);
mysql_select_db("angular") or die(mysql_error());
mysql_query("INSERT INTO story (subject,body) VALUES ('$subject', '$body')");
Print "Your information has been successfully added to the database.";

$query = "SELECT * FROM story";
$result = mysql_query($query);

$arr = array();
while ($row = mysql_fetch_array($result)) {
$subject = $row['subject'];
$body = $row['body'];
$arr[] = $row;
}
echo json_encode($arr);

json

<b>Notice</b>:  Trying to get property of non-object in <b>D:\xampp\htdocs\Angular\factory\insert.php</b> on line <b>14</b><br />
Your information has been successfully added to the database.[{"0":"","subject":"","1":"","body":""},{"0":"","subject":"","1":"","body":""},{"0":"Soheil","subject":"Soheil","1":"Sadeghbayan","body":"Sadeghbayan"},{"0":"adsas","subject":"adsas","1":"asdasdasda","body":"asdasdasda"},{"0":"Say","subject":"Say","1":"Something","body":"Something"},{"0":"asd","subject":"asd","1":"asdasdasd","body":"asdasdasd"},{"0":"asda","subject":"asda","1":"dasdasd","body":"dasdasd"},{"0":"asd","subject":"asd","1":"asdadd","body":"asdadd"},{"0":"asaS","subject":"asaS","1":"saAS","body":"saAS"},{"0":"adasda","subject":"adasda","1":"dasdasdasdasdasdasd","body":"dasdasdasdasdasdasd"},{"0":"AS","subject":"AS","1":"sASasAS","body":"sASasAS"},{"0":"asd","subject":"asd","1":"asdasd","body":"asdasd"},{"0":"xZ","subject":"xZ","1":"zXzXZX","body":"zXzXZX"},{"0":"weqe","subject":"weqe","1":"qeqeqe","body":"qeqeqe"},{"0":"gf","subject":"gf","1":"kjh","body":"kjh"},{"0":"","subject":"","1":"","body":""},{"0":"","subject":"","1":"","body":""},{"0":"","subject":"","1":"","body":""},{"0":"","subject":"","1":"","body":""}]

这段代码中我缺少什么来显示数据?提前谢谢

最佳答案

您没有从 $http 返回值。

IMO,这是一种更好的设置方法,因为 $http 返回一个 promise 。

fruitsApp.factory('fruitsFactory', function($http) {
return {
getFruitsAsync: function() {
return $http.get('insert.php');
}
};
});

fruitsApp.controller('fruitsController', function($scope, fruitsFactory) {
fruitsFactory.getFruitsAsync()
.then(function (response) { // Leveraging the .then from $http promise.
$scope.fruits = response.data.fruits;
});
});

根据您的 JSON 添加进行更新:

您的 JSON 看起来不像有效的 JSON。

{
"fruits": [
{"0":"","subject":"","1":"","body":""},
{"0":"","subject":"","1":"","body":""},
{"0":"","subject":"Soheil","1":"Sadeghbayan","body":"Sadeghbayan"}
]
}

这样更有意义,让水果中的水果最终成为水果集合中的每个对象(模型)。

关于javascript - 在 AngularJS 中使用工厂显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26225816/

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