2017-08-29 9 views
0

SQLからデータを取得しようとしましたが、機能しません。SQLからのデータがAngularJS内にロードされていません

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

http getは非同期ですが、response.dataを待たずに$scope.eventsを実行し続けるので、$scope.calendars.calendar_titleはまだこのタイミングでは定義されていません。

$scope.eventsthenに移動してください。

$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/'} 
    ]; 
}); 
+0

ありがとうございました。しかし、私があなたの方法を試してみると、関数全体がデータを取得しません。なぜなのかご存知ですか? – Jay

+0

SQLテーブルとカラム名が「calendar_title」の場合、$ scope.calendars.calendar_titleを呼び出す適切な方法はありませんか? – Jay

関連する問題