angularJs ng-repeat 中的响应数组
angularJs response array in ng-repeat
我有一个 API 正在响应 array.And 我想用 ng-repeat 列出。
这里是phpApi代码
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, First_Name, Last_time,Email,Phone_Number,Location,Phd_Degree,Phd_University,MS_Degree,MS_University,BS_Degree,BS_University FROM user";
$result = $conn->query($sql);
$data=array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$data[]=array(
'id'=>$row['id'],
'First_Name'=>$row['First_Name'],
'Last_time'=>$row['Last_time'],
'Email'=>$row['Email'],
'Phone_Number'=>$row['Phone_Number'],
'Location'=>$row['Location'],
'Phd_Degree'=>$row['Phd_Degree'],
'Phd_University'=>$row['Phd_University'],
'MS_Degree'=>$row['MS_Degree'],
'MS_University'=>$row['MS_University'],
'BS_Degree'=>$row['BS_Degree'],
'BS_University'=>$row['BS_University']
);
//echo "id: " . $row["id"]. " - Name: " . $row["First_Name"]. " " . $row["Last_time"]. "
";
}
} else {
$data[]=array(
'message'=>'not success',
);
}
print_r($data);
//return $dat=json_encode($data);
$conn->close();
?>
这是我的 angular 代码。
app.controller('listuser', function ($scope,$http,$log) {
$scope.data=[];
return $http({
method: 'POST',
url: 'apisource.php',
})
.then(function (results) {
$scope.data=results.data;
$log.log($scope.data);
});
});
现在的问题是,当我尝试在 html 上实施时,它不起作用
这里是 html 代码
{{x.First_Name}}
我认为问题出在我的回复中,因为它不是 Json 格式,但是当我尝试执行 json_encode() 时,回复中显示空白。
这是我的控制台中显示的响应。
Array
(
[0] => Array
(
[id] => 1
[First_Name] => Zachary
[Last_time] => Chatha
[Email] => kouda@gmail.com
[Phone_Number] => 7745752121
[Location] => Guden�vej 1, 20 Vanl�se, Denmark, Apt 2
[Phd_Degree] => test
[Phd_University] => test
[MS_Degree] => Master of Science in Pharmaceutical Sciences (MSPS)
[MS_University] => Copenhagen University (Denmark)
[BS_Degree] => \nBachelor of Science, Biological Applications and Technologies\n
[BS_University] => University of Ioannina (Greece)
)
[1] => Array
(
[id] => 2
[First_Name] => Gaurav
[Last_time] => Kumar
[Email] => gtest@gmail.com
[Phone_Number] => 7745752121
[Location] => Guden�vej 1, 20 Vanl�se, Denmark, Apt 2
[Phd_Degree] => test
[Phd_University] => test
[MS_Degree] => Master of Science in Pharmaceutical Sciences (MSPS)
[MS_University] => Copenhagen University (Denmark)
[BS_Degree] => \nBachelor of Science, Biological Applications and Technologies\n
[BS_University] => University of Ioannina (Greece)
)
)
在 php 中打印 json header,而不是返回:
header('Content-Type: application/json');
echo json_encode($data);
我有一个 API 正在响应 array.And 我想用 ng-repeat 列出。
这里是phpApi代码
connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, First_Name, Last_time,Email,Phone_Number,Location,Phd_Degree,Phd_University,MS_Degree,MS_University,BS_Degree,BS_University FROM user"; $result = $conn->query($sql); $data=array(); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $data[]=array( 'id'=>$row['id'], 'First_Name'=>$row['First_Name'], 'Last_time'=>$row['Last_time'], 'Email'=>$row['Email'], 'Phone_Number'=>$row['Phone_Number'], 'Location'=>$row['Location'], 'Phd_Degree'=>$row['Phd_Degree'], 'Phd_University'=>$row['Phd_University'], 'MS_Degree'=>$row['MS_Degree'], 'MS_University'=>$row['MS_University'], 'BS_Degree'=>$row['BS_Degree'], 'BS_University'=>$row['BS_University'] ); //echo "id: " . $row["id"]. " - Name: " . $row["First_Name"]. " " . $row["Last_time"]. "
"; } } else { $data[]=array( 'message'=>'not success', ); } print_r($data); //return $dat=json_encode($data); $conn->close(); ?>
这是我的 angular 代码。
app.controller('listuser', function ($scope,$http,$log) { $scope.data=[]; return $http({ method: 'POST', url: 'apisource.php', }) .then(function (results) { $scope.data=results.data; $log.log($scope.data); }); });
现在的问题是,当我尝试在 html 上实施时,它不起作用
这里是 html 代码
{{x.First_Name}}
我认为问题出在我的回复中,因为它不是 Json 格式,但是当我尝试执行 json_encode() 时,回复中显示空白。
这是我的控制台中显示的响应。
Array ( [0] => Array ( [id] => 1 [First_Name] => Zachary [Last_time] => Chatha [Email] => kouda@gmail.com [Phone_Number] => 7745752121 [Location] => Guden�vej 1, 20 Vanl�se, Denmark, Apt 2 [Phd_Degree] => test [Phd_University] => test [MS_Degree] => Master of Science in Pharmaceutical Sciences (MSPS) [MS_University] => Copenhagen University (Denmark) [BS_Degree] => \nBachelor of Science, Biological Applications and Technologies\n [BS_University] => University of Ioannina (Greece) ) [1] => Array ( [id] => 2 [First_Name] => Gaurav [Last_time] => Kumar [Email] => gtest@gmail.com [Phone_Number] => 7745752121 [Location] => Guden�vej 1, 20 Vanl�se, Denmark, Apt 2 [Phd_Degree] => test [Phd_University] => test [MS_Degree] => Master of Science in Pharmaceutical Sciences (MSPS) [MS_University] => Copenhagen University (Denmark) [BS_Degree] => \nBachelor of Science, Biological Applications and Technologies\n [BS_University] => University of Ioannina (Greece) ) )
在 php 中打印 json header,而不是返回:
header('Content-Type: application/json');
echo json_encode($data);