【问题标题】:angular ngResource pass additional arguments to the url角度 ngResource 将附加参数传递给 url
【发布时间】:2015-03-23 07:16:07
【问题描述】:

我有一个项目资源,包含多个任务资源(类似于 Facebook 帖子和 cmets 的关系)

当我更新一个项目时,我想使用 url /project/[project id]。当我更新任务时,我想使用 url /project/[project id]/[task id]

这里是我的项目资源服务:

angular.module('project').factory('Project', ['$resource', function($resource) {

    return $resource('project/:projectId', {
        projectId: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    })
}])

现在我要定义任务资源:

angular.module('task').factory('Task', ['$resource', function($resource) {

    return $resource('project/:projectId/:taskId', {
        projectId: '' //how can i pass in the project Id from controller? 
        taskId: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    })

}])

在我的控制器中,如果我只是更新项目,而不是它的任务,那么我只需使用:

    $scope.update = function() {

        $scope.project.$update(function() {
            $location.path('project/' + $scope.project._id)
        }, function(errResponse) {
            $scope.error = errorResponse.data.message
        })
    }

但是当我更新项目的任务时,我想传入项目来构造url。我怎样才能做到这一点?

请注意,项目和任务属于不同的模块。用户可以在项目详情页面或任务详情页面中更新任务。

【问题讨论】:

    标签: angularjs node.js ngresource


    【解决方案1】:

    工厂应该是这样的:

    angular.module('task').factory('$task',function($resource){
            return $resource('project/:id/:taskid',
            {
                    id: '@id',
                    taskid: '@taskid'
            },{
                    'update': {method: 'PUT'},
                    'list': {method: 'GET'},
                    'create':{method: 'POST'},
                    'delete': {method: 'DELETE'}
            });
    });
    

    在控制器中调用这个工厂:

    $task.list({
        id : 1, // json prop name should be same as declared in factory
        taskid : 10
    });
    

    这会将ajax请求发送到:/project/1/10

    如果您想更新任务,您必须提供其项目 ID。

    【讨论】:

      【解决方案2】:

      我不知道你在做什么。

      为特殊项目首先使用项目模块“获取”更聪明。

      如果你有这个对象,你可以更新它。

      看起来像这样:

      angular.module('project').factory('Project', ['$resource', function($resource) {
      
          return $resource('project/:projectId', {
              projectId: '@_id'
          }, {
              update: {
                  method: 'PUT'
              },
      
              get: {
                  method:'GET'
                  }
          })
      }])
      
      
      
      
      Project.get({projectId: projectId}, function(response){
      
      //the response contains the data of the project Id 
      //I cant test it now, ....check the docu.
      //now u could update and take the id still from the response
      
      
      
      
      })
      

      【讨论】:

        【解决方案3】:

        传递它的方式与传递其他命名参数的方式相同:

        angular.module('task').factory('Task', ['$resource', function($resource) {

        return $resource('project/:projectId/:taskId', {
            projectId: '@pid', 
            taskId: '@_id'
        }, {
            update: {
                method: 'PUT'
            }
        });
        

        然后,您的数据模型将具有 _idpid ng 将跟踪的属性,即:

        this.projectId = 17 // or however you set it.
        var task = new Task({
          _id: '213',
          pid: this.projectId
        });
        task.save(); // POST /project/17/213
        

        看到了吗?所以 $resource 的第二个参数是 params。您映射到 URL 的那些将转到 URL,其他在请求正文(或查询字符串,取决于)。

        【讨论】:

          猜你喜欢
          • 2013-12-25
          • 1970-01-01
          • 2014-07-30
          • 1970-01-01
          • 2011-12-21
          • 2013-01-09
          • 1970-01-01
          • 2019-05-05
          • 2019-08-22
          相关资源
          最近更新 更多