2016-01-22 61 views
0

我正在嘗試爲PHP後端基礎應用程序構建一個基於AngularJS的前端。我使用PHP & MySQL查詢構建JSON對象。 JSON被正確導出,並且如果我使用與ng-init指令內聯的objet,則會顯示數據。但是,當我在控制器上安裝Json.php文件時,似乎出現錯誤,因爲我沒有看到任何數據。從PHP獲取JSON與PHP的AngularJS沒有顯示在控制器上

This is指向示例的鏈接,此處的控制器位於</body>標記下方。

這是主要的網頁

<html ng-app="myApp"> 
    <head> 
     <title></title> 
    </head> 
    <body> 
     <div style="direction: ltr"> 

     </div> 
     <h1>תוצאות שאלון</h1> 
     <div class="table-responsive" ng-controller="ContentCtrl">   

     <table class="table" id="results" > 
      <thead> 
       <tr> 
        <th>id</th> 
        <th>q1</th> 
        <th>q2</th> 
        <th>q3</th> 
        <th>textarea</th> 
        <th>phone</th> 
        <th>name</th> 
       </tr> 
      </thead> 
      <tbody> 
<tr ng-repeat="answer in answers.data"> 

        <td>{{answer.id}}</td> 
        <td>{{answer.q1}}</td> 
        <td>{{answer.q2}}</td> 
        <td>{{answer.q3}}</td> 
        <td>{{answer.textarea}}</td> 
        <td>{{answer.phone}}</td> 
        <td>{{answer.name}}</td> 
</tr> 



       <?php/* 
       $table = new Results(); 
       $table->allrows(); 
       */?> 
      </tbody> 
     </table> 
     </div> 
     <script src="http://code.jquery.com/jquery.js"></script> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
     <script type="text/javascript" src="js/js.js"></script> 
     <!--<script src="js/controllers.js"></script>--> 
     <script type="text/javascript"> 

這是控制器:

var myApp = angular.module('myApp', []); 

myApp.controller('ContentCtrl', ['$scope', '$http', function ($scope, $http) { 
    $http.get('json.php') 

    .success(function(data) { 

     $scope.answers = data; 

     console.log('test'); 
    }); 
}]); 

也許模塊應該另一頁上? 我之前問過this question。也許它可以幫助某種方式。

+0

在您的鏈接示例頁面的'json.php'給出了500. –

+0

對不起,我對服務器進行了更新,這是新的鏈接http://seveloff.com/survey/json.php,這是到頁面http:// seveloff的新鏈接。 com/survey/results.php – DavSev

回答

0

您的json.php文件響應的JSON只是一個對象數組。但是你的ng-repeat正在迭代answers.data,這是未定義的。你應該迭代answers,因爲這是數組。

<tr ng-repeat="answer in answers"> 

如果由於某種原因,你不想改變ng-repeat,那麼你就可以改變你的控制器,以便它在這樣一個對象包裝data

$scope.answers = { data: data }; 
+0

謝謝。所以你建議我做什麼?我怎樣才能將json.php中的JSON綁定到控制器中的answers.data中 – DavSev

+0

爲什麼不像我顯示的那樣改變你的ng-repeat呢? –

+0

@DavSev編輯。 –