小编Alf*_*red的帖子

在$ resource中使用拦截器

我如何interceptor在Angular中使用$resource

我的JSON结构:

var dgs = [{id    :1,
            driver:'Sam',
            type:  'bus',
            segments:[{id:1,origin:'the bakery',arrival:'the store'},
                      {id:2,origin:'the store' ,arrival:'somewhere'}]
            },
            { ... },
            { ... }
          ];
Run Code Online (Sandbox Code Playgroud)

我的控制器如下:

function dgCtrl($scope,$http,DriveGroup,Segment) {
  $scope.dgs = DriveGroup.query(function()
    // Code below may belong in a response interceptor?
    for (var i=0;i<$scope.dgs.length;i++) {
      var segments = $scope.dgs[i].segments;
      for (var j=0;j<segments.length;j++) {
        segments[j] = new Segment(segments[j]);
      }
    }
  });
Run Code Online (Sandbox Code Playgroud)

我的服务,以及我尝试使用该interceptor对象:

angular.module('dgService',['ngResource']).
  factory("DriveGroup",function($resource) {
    return $resource(
      '/path/dgs',
      {},
      {update:{method:'PUT'})
      {fetch :{method:'GET',
               // This is what …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs

9
推荐指数
1
解决办法
8270
查看次数

标签 统计

angularjs ×1

javascript ×1