dpbfb7119 2017-10-13 03:57
浏览 62
已采纳

获取JSON数据到angularJS模块[重复]

This question already has an answer here:

I am trying to bind json data to angular module. Following is my code.

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

And my list.php file,

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

But it is now displaying list in HTML file. What can be the reason? Is their any special way to create json objects for angularJS?

</div>
  • 写回答

1条回答 默认 最新

  • duanmao9918 2017-10-13 04:07
    关注

    you should write response.data instead of response.data.records. as your JSON is an array not object.

    See $http service response object properties.

    Here is your updated code.

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的