我需要在工厂内获取此资源:
Content: $resource("http://localhost:3000/matches/:id/content")
但是当我尝试使用以下方式访问它时:
Content.get({id: $scope.match.id}, function(){});
我收到错误,因为它试图达到“/ matches / content”而不是“/ matches / 123 / content”(假设id为123)。
GET http://localhost:3000/matches/content 404 (Not Found)
我该如何解决这个问题?
答案 0 :(得分:3)
这里应该工作得很好是测试它的小提琴。你能过早称呼它吗?
http://jsfiddle.net/mbkq12wu/1/
angular.module('userApp', ['ngResource'])
.factory('Content', function($resource) {
return $resource("http://localhost:3000/matches/:id/content");
});
describe('SO Content Test resource calls matches/1/content', function () {
it('Calls GET – api/:id/content', function() {
$httpBackend
.expectGET('http://localhost:3000/matches/1/content').respond(200);
Content.get({id: 1}, function(){});
$httpBackend.flush();
});
答案 1 :(得分:1)
你的代码很好。问题是$scope.match.id
解析为undefined
,导致资源网址被忽略:id
部分(并且已删除重复的/
)。
验证资源是否可以检查
Content.get({id: 123}, function(){});
以及console.log($scope.match.id)
。