我想在页面中显示db中的数据。
这是我的代码
JS:

 $scope.save = function() {
   var data = {
     subject: $scope.composeStory.subject,
     body: $scope.composeStory.body
   }


   $http.post(
       "insert.php", {
         'subject': $scope.composeStory.subject,
         'body': $scope.composeStory.body
       }
     )
     .success(function(data, status, headers, config) {
       console.log("inserted Successfully");
     });
 };

和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['name'];
    $body = $row['description'];
    $arr[] = $row;
}
echo json_encode($arr);

杰森
[{"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"}]

它保存到数据库完美,但我不知道如何显示数据库中的数据到我的页面?

最佳答案

要检索数据,请创建工厂服务,该服务将使用$http GET方法,其中url指向您的php文件,该文件将以以下格式返回$data数组:

这是我在另一个问题上发布的最新示例:

demoApp.factory('simpleFactory', ['$http', function($http){

    return {

        getCustomer: function(){

            return $http.get("json/customers.php").then(
                function(response){
                    var customers = [];
                    customers = response.data;
                },
                function(error){
                    console.log(error);
                });
        }
    }
}]);

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

10-12 18:09
查看更多