2014-01-09 38 views
0

我是角和rails的新手。我試圖從數據庫中獲取用戶,並希望在屏幕上顯示它作爲索引頁。我能夠在滑軌控制器獲取數據,但在角控制器漸漸空虛數據沒有從角度js的resouce.query中獲取

角控制器:

app = angular.module("userApp", ["ngResource"]) 
app.controller("userController", function($scope, $resource, $http){ 
User = $resource('/users', {}, {query: {method: "GET", isArray: true}}) 
    $scope.users = User.query() 
    alert($scope.users) 
}); 

Rails的控制器: 類UsersController <的ApplicationController respond_to代碼:HTML

def index 
@user = User.all 
@user.each do |u| 
    puts u.name 
end 
    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @user } 
    end 
end 
end 

我getter $ scope.users爲空。 請幫我解決這個問題。

感謝, JK

回答

0

您試圖訪問該資源之前,它已準備就緒。使用成功回調代替

... 
app.controller("userController", function($scope, $resource, $http){ 
... 
    User.query({}, function(data){ 
     $scope.users = data; 
     alert($scope.users) 
    }) 
}); 
+0

感謝您的迴應。我試過這種方式。我仍然沒有收到任何數據。更多的警報消息不會來。 – Jagan