簡體   English   中英

如何使用ngResource訪問資源URL參數?

[英]How to get access to resource URL params with ngResource?

我有一個使用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>

這樣的事情看起來還好嗎?

Java腳本

//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>

只需將區域ID放在范圍內,例如: $scope.aid = $routeParams.aid; ,並在任何需要的地方引用它。 當您使用單一aid進行查詢時,對所有建築物來說都一樣嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM