只是尝试使用 angularjs$resource
服务获得一个非常简单的 REST get 调用。永远不会调用 API。这是代码:
angular.module('app.services', [])
.service('tourRepository', function ($resource) {
this.get = function (id) {
alert('tourRepositoryCalled for id ' + id + '!'); // this is called
var tour = $resource('/api/tour/:tourid', { tourId: '@id' });
return tour.get({ tourId: id }, function () {
alert('tour api called.'); // THIS NEVER IS
});
}
如果我使用浏览器并转到 /api/tour/3,它可以工作。我在这里做错了什么?我认为 $resource 调用可能会更容易,例如如下所示,但我正在关注 angularjs 文档。
理想情况下:
$scope.tour = $resource.get('/api/tour/' + id); // why wouldn't this work?
我要做的就是调用一个简单的 REST 服务并将结果分配给我的 $scope.tour 对象。由于我在服务中(我的“存储库”),我无权访问 $scope。解决这个问题的最佳方法是什么?当我将代码更改为 API 下方时,就会受到影响。
var tour = $resource('/api/tour/:tourid');
var theTour = tour.get({ tourid: id });
alert(theTour.id);