【发布时间】:2013-03-21 07:11:33
【问题描述】:
我正在使用带有 angularjs 的 rails 3.2,我正在尝试了解如何使用 angular 的 $resource。
我希望资源从非默认操作返回结果(因此不获取、保存、查询、删除和删除)
所以在我的帖子控制器中我有
def home
@recent_posts = Post.recent_posts
respond_to do |format|
format.html
format.json
end
end
然后在我的 js 文件中
app.factory "Post", ["$resource", ($resource) ->
$resource("/post/:id/:action", {id: "@id"}, {home: {method: "GET", isArray: true}})
]
@HomeCtrl = ["$scope", "Post", ($scope, Post) ->
$scope.recent_posts = Post.home()
]
在我看来,如果我尝试执行ng-repeat="post in recent_posts" 之类的操作,它不会返回任何内容。
【问题讨论】:
-
$scope.recent_posts = Post.home({action:'home', function(response){console.log(response) }, function(response){console.log(response)} })。请注册这些回调并检查响应中的内容。第一个函数是成功回调,第二个是错误回调。