Php 将JSON数据获取到angularJS模块
我正在尝试将json数据绑定到angular模块。下面是我的代码Php 将JSON数据获取到angularJS模块,php,mysql,angularjs,json,Php,Mysql,Angularjs,Json,我正在尝试将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>
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>
var-app=angular.module('myApp',[]);
app.controller('myCtrl',函数($scope,$http){
$http.get(“list.php”).then(函数(响应){
$scope.myData=response.data.records;
});
});
-
{{z.Type}
和我的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是一个数组
而不是对象
请参阅$http
服务
这是您的更新代码
<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>
var-app=angular.module('myApp',[]);
app.controller('myCtrl',函数($scope,$http){
$http.get(“list.php”).then(函数(响应){
$scope.myData=response.data;
});
});
-
{{z.Type}
你能粘贴你的JSON,它通过你的API返回吗?[{“code”:“1”,“Type”:“Luxuary”,“MinFee”:“2000.00”,“DayRate”:“50.00”},{“code”:“2”,“Type”:“sample”,“MinFee”:“300.00”,“DayRate”:“200.00”},{“code”:“3”,“Type”:“Economy”,“MinFee”:“2000.00”,“DayRate”:“45.00”},{“code”:“code”:“4”,“Type”:“sdf”,“MinFee”:“456“sfdsdfsdf”、“MinFee”:“2323.00”、“DayRate”:“2323.00”}]