2014-10-17 131 views
1

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


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

做工精细:

$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); 
}; 

我得到的错误:不确定是不是我的$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…} 

我究竟该如何解决呢?

+0

什么是'$ scope.answerEdited'? – tymeJV 2014-10-17 20:29:26

+0

当前正在编辑的答案。它等于在console.log中返回的Resource元素,所以它看起来好像有正确的数据和所有内容 – 2014-10-17 20:36:40

+0

你是否已经用madal定义了答案,即具有正确类型的模式? – invinciblejai 2014-10-20 13:57:01

回答

2

您可以尝试以下方法并检查它是否有效?

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

    $scope.toggleEditAnswer(answer); 
};