2016-03-24 39 views
1

我正在AngularJS中開發一個webapp。我創建了一個用於發送json請求並通過post發送json數據的服務。該服務是好的,因爲當我加載頁面Firebug顯示我JSON的數據,但視圖不加載數據。AngularJS服務沒問題,但沒有數據

這是我的代碼

var app = angular.module('MyApp', ['ngRoute']); 

    app.service('selectSrv',['$http', function($http){ 
     this.select = function(tab, fields){ 
       $http.defaults.headers.post["Content-type"] = "application/json"; 
       return $http.post('/loader',{posttab:tab, postfields:fields}) 
         .success(function(data){ 
          return data; }); 
      }; 
    }]); 
    app.config(['$routeProvider',function($routeProvider){ 
     $routeProvider 
      .when("/report",{ 
          templateUrl:"/reportV.html", 
          controller:"reportController", 
          resolve:{ 
            dati: function(selectSrv){ 
             return selectSrv.select(table,["col1","  }; 
}]); 
col2","col3"]); } 
          } }) 
      .otherwise({redirectTo:'/'}); 

這是我reportController.js

app.controller("reportController", ['$scope','$log', function($scope, $log, dati){ 
    $scope.rows = dati; }]); 

,這是我reportV.html

<div class="col-md-8"> 
    <table class="table table-striped"> 
     <thead> 
      <tr> 
       <th>Code</th> 
       <th>Surname</th> 
       <th>Name</th> 
      </tr> 
     </thead> 
     <tbody ng-repeat="row in rows"> 
      <tr> 
       <td>{{row.code}}</td> 
       <td>{{row.surname}}</td> 
       <td>{{row.name}}</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

另外,如果我寫的日誌我請參閱「未定義」,而不是看日誌上的數據。

對所有人的問候。

Davide

回答

0

你忘了在你的數組表示法中聲明已解決的依賴關係。應該是:

app.controller("reportController", ['$scope', '$log', 'dati', function($scope, $log, dati) { 
    $scope.rows = dati;    // note this DI --^ 
}]); 
+0

ooppss。謝謝@dfsq你是對的,但現在我看到一個壓縮的空表..... – dpope

+0

好吧,它的工作原理。我不知道這是必要的過濾器dati.data Enjoy – dpope

+0

'data'是必需的,因爲$ http服務返回響應對象與一堆東西,所以它需要將實際響應數據包裝到某個東西中,所以它聲明'data'屬性。在你的情況下,而不是'成功'使用'.then(函數(響應){返回response.data;})'。如果有幫助,請記住接受答案:)。 – dfsq