Angular $更新资源

时间:2014-10-17 20:26:40

标签: javascript angularjs angular-resource

编辑:如果在没有发布更多代码的情况下很难排除故障,请告诉我,我可以在问题中添加更多内容。


我有两个基本相同代码的实例,其中一个是工作而另一个不是。

工作正常:

$scope.update = function() {
  var question = $scope.question;
  question.$update(function() {
    $location.path('questions/' + question._id);
  });
};

无效:

$scope.updateAnswer = function() {
  var answer = $scope.answerEdited;
  answer.$update(function() {
    $location.path('questions/' + $routeParams.questionId);
  });

  $scope.toggleEditAnswer(answer);
};

我收到错误: undefined不是我$update函数中的函数。这是我的资源:

angular.module('intquestApp')
  .factory('Answers', function ($resource) {
    return $resource('api/answers/:answerId', {
      answerId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  });

我究竟做错了什么?如果我正确理解了流程,answer.$update会以某种方式使用资源吗?另外,如果我使用console.log来查看正在更新的answer,那么它是正确的:

Resource {_id: "543d8896039390eb5e000001", created: "2014-10-14T20:33:26.514Z", creator: Object, questionid: "543d8693994fc1685d000001", __v: 0…}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您可以尝试以下操作并检查它是否有效吗?

$scope.updateAnswer = function() {
    Answers.update($scope.answerEdited, function(response) {
        $location.path('questions/' + $routeParams.questionId);
    });

    $scope.toggleEditAnswer(answer);
};
相关问题