簡體   English   中英

使用AngularJS顯示數據庫中的數據

[英]Showing data from database with AngularJS

我想在頁面中顯示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"}]

它完美地保存到db,但是我不知道如何從數據庫向頁面顯示數據?

為了檢索數據,創建一個工廠服務,該服務將使用$http GET方法,其url指向您的php文件,該文件以以下格式返回$data數組: echo json_encode($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);
                });
        }
    }
}]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM