This question already has an answer here:
Angular get JSON data with ajax
                                
                                    (1个答案)
                                
                        
                                2年前关闭。
            
                    
我正在尝试将json数据绑定到angular模块。以下是我的代码。

<script>
  var app = angular.module('myApp',[]);
  app.controller('myCtrl',function($scope,$http){
    $http.get("list.php").then(function(response) {
        $scope.myData = response.data.records;
    });
  });
</script>

    <div class="col-md-12"><!--col 1-->
       <ul>
        <li ng-repeat="z in myData">
          {{ z.Type }}
        </li>
      </ul>


还有我的list.php文件,

<?php
include_once("mysql_db/db.php");
$sql = "SELECT * FROM taxitype";
$data = retrieve($sql); // This execute mysqli functions and return an array
echo json_encode($data);
?>


但是它现在在HTML文件中显示列表。可能是什么原因?
他们为angularJS创建json对象有什么特殊的方法吗?

最佳答案

您应该写response.data而不是response.data.records。因为您的JSON是array而不是object

请参见$http服务response object properties

这是您更新的代码。

<script>
  var app = angular.module('myApp',[]);
  app.controller('myCtrl',function($scope,$http){
    $http.get("list.php").then(function(response) {
        $scope.myData = response.data;
    });
  });
</script>

    <div class="col-md-12"><!--col 1-->
       <ul>
        <li ng-repeat="z in myData">
          {{ z.Type }}
        </li>
      </ul>

关于php - 将JSON数据获取到angularJS模块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46722315/

10-14 13:51
查看更多