[英]How to get access to resource URL params with ngResource?
I have an AngularJS service for handling buildings, using ngResource: 我有一个使用ngResource处理建筑物的AngularJS服务:
angular.module('neighborhoodApp')
.factory('Building', function ($resource) {
return $resource('/areas/:aid/buildings/:id', {
'id': '@_id'
});
});
In one of my view, I list the buildings: 在我的观点之一中,我列出了这些建筑物:
$scope.buildings = Building.query({
aid: $routeParams.aid
});
Note that I am getting the area id from the route, but this information is not stored inside my model. 请注意,我从路线中获取区域ID,但是此信息未存储在模型中。
My issue is that when I display my buildings, I would like to use the area id but I don't have access to it: 我的问题是,当我显示建筑物时,我想使用区域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>
Something like this seems ok ? 这样的事情看起来还好吗?
Javascript 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 HTML
<pre>AREAID: {{ buildings.id }}</pre>
<div ng-repeat="building in buildings.resource">
{{building | json}}
</div>
Simply put the area Id on the scope, eg: $scope.aid = $routeParams.aid;
只需将区域ID放在范围内,例如:
$scope.aid = $routeParams.aid;
, and reference it where ever you want. ,并在任何需要的地方引用它。 As you are using the single
aid
for the query, won't it be the same for all buildings? 当您使用单一
aid
进行查询时,对所有建筑物来说都一样吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.