这是我的php代码
<?php
try {
$dbcon=new PDO('mysql:host=localhost;dbname=angular;charset=utf8',"root","");
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbcon->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
$query="SELECT * FROM products ";
$sql=$dbcon->prepare($query);
$sql->execute();
$result=$sql->fetchAll(PDO::FETCH_OBJ);
$json_result=json_encode($result);
echo $json_result;
}catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
这是我的控制器到角度
function ProductListCtrl($http)
{
$http.get('api/products.php').success(function (data) { alert(data); this.product = data; });
}
报警信息是【对象对象,...】,如何从PHP中检索json数据?
$http.get 与 JSON 配合使用可以正常工作我建议您验证您的 json 并再次测试我为您创建了一个带有$http和ng重复的简单示例
<!doctype html>
<html ng-app="test">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<script>
var app = angular.module('test', []);
app.controller('test', function($scope, $http) {
$http.get('test.json').success(function(data){
console.log(data);
$scope.items = data;
});
});
</script>
</head>
<body ng-controller="test">
<div ng-repeat="item in items">{{item.name}}</div>
</body>
</html>
并且 JSON 文件是
[{
"name":"name1"
},
{
"name":"name2"
}]
还有一个在普伦克观看直播的链接http://plnkr.co/edit/MN6UPg1ba1OYNFNHKMdG?p=preview
我解决了这个问题。错误在于此产品
(function ()
{
"use strict";
var app = angular.module('ProductList');
app.controller('ProductListCtrl',['$scope','$http', ProductListCtrl]);
function ProductListCtrl($scope, $http) {
$http.get('api/products.php').success(function (data) { $scope.product = data; });
}
}());