我试图从SQL中检索一些数据,但它不起作用。
我的PHP工作正常。
我认为ANGULARJS控制器存在一些问题。
以下是代码:
[PHP]
require_once 'connect.php';
$query = $conn->query("SELECT * FROM `calendars`") or die(mysqli_error());
$data = array();
while($row = $query->fetch_array()){
$data[] = $row;
}
echo json_encode($data);
[AngularJS控制器]
function CalendarCtrl($scope,$http) {
$http.get('../crud/calendars_read.php').then(function(response){
//$scope.calendar = [];
$scope.calendars = response.data;
});
$scope.events = [
{title: $scope.calendars.calendar_title,
start: new Date(2017, 8, 2)},
// This Part does not work.
{title: 'Click for Google',start: new Date(y, m, 28),end: new Date(y, m, 29),url: 'http://google.com/'}
// This Part works well if there is no $scope calling as above.
];
我认为$ scope.event在数组中,这就是$ scope.calendars无法正常工作的原因。谁能帮助我如何检索
$scope.calendars.calendar_title
从查询到数组?
提前谢谢你,祝你有个美好的一天!
答案 0 :(得分:0)
http get是异步的,它将继续运行$scope.events
而不等待response.data
,因此此时尚未定义$scope.calendars.calendar_title
。
尝试将$scope.events
移至then
函数。
$http.get('../crud/calendars_read.php').then(function(response){
//$scope.calendar = [];
$scope.calendars = response.data;
$scope.events = [
{title: $scope.calendars.calendar_title,
start: new Date(2017, 8, 2)},
{title: 'Click for Google',start: new Date(y, m, 28),end: new Date(y, m, 29),url: 'http://google.com/'}
];
});