.query()渲染错误的路径

时间:2015-08-26 05:45:01

标签: javascript angularjs

我的功能是使用query()方法显示所有任务。问题在于它指的是users/tasks?status=completed

我需要users/:user_id/tasks?status=completed。  我该如何解决?

var app = angular.module('Todolist', ['ngResource']);

  app.factory('Task', [
    '$resource', function($resource) {
      return $resource('/users/:user_id/tasks/', {id: '@id'}, {update: {method: 'PUT'}});
     }
   ]);

  app.controller('TasksCtrl', [
    '$scope', 'Task', function($scope, Task) {

      $scope.user = current_user

      $scope.tasks = Task.query(
        status: 'incompleted'
      );

    }
   }
  ]);

1 个答案:

答案 0 :(得分:1)

请更新您的控制器

 app.controller('TasksCtrl', [
    '$scope', 'Task', function($scope, Task) {
     $scope.user = null; //current_user please define this variable it will cause for undefined error
    $scope.tasks = Task.query({status: 'completed', user_id : 12}); //'12' is user id , you can pass dynamically

 }]);

工厂代码是:

app..factory('Task', [
    '$resource', function($resource) {
      return $resource('/users/:user_id/tasks/',
      {user_id: '@user_id'},
      {update: {method: 'PUT'}});
   }
]);

Working plunker