2

我有一个使用 ngResource 处理建筑物的 AngularJS 服务:

angular.module('neighborhoodApp')
  .factory('Building', function ($resource) {
    return $resource('/areas/:aid/buildings/:id', {
      'id': '@_id'
    });
  });

在我的一种观点中,我列出了这些建筑物:

$scope.buildings = Building.query({
  aid: $routeParams.aid
});

请注意,我从路线中获取区域 ID,但此信息未存储在我的模型中。

我的问题是,当我显示我的建筑物时,我想使用区域 ID,但我无权访问它:

<div ng-repeat="building in buildings">
  {{building | json}}
  <!-- 'building' doesn't include the area id
       I can't modify the server response and somehow
       I shouldn't have to, the information is inside the URL of the query()
       Can I get URL params from the resource instance? -->
</div>
4

2 回答 2

0

像这样的东西似乎没问题?

Javascript

//Service
angular.module('neighborhoodApp')
      .factory('Building', function($resource) {
        return {
            query: function(id) {
                aid: id,
                resource: $resource('/areas/:aid/buildings/:id', {
                    'id': id
                });
            }
        }
});

// Invokation
$scope.buildings = Building.query({
  aid: $routeParams.aid
});

HTML

<pre>AREAID: {{ buildings.id }}</pre>
<div ng-repeat="building in buildings.resource">
  {{building | json}}
</div>
于 2014-02-19T13:58:36.170 回答
0

只需将区域 ID 放在范围上,例如:$scope.aid = $routeParams.aid;,然后在任何你想要的地方引用它。当您使用单aid进行查询时,所有建筑物都不是相同的吗?

于 2014-02-19T14:03:53.870 回答