2014-10-17 133 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); 
};