简体   繁体   English

角$ update资源

[英]Angular $update resource

EDIT: If it's difficult to troubleshoot without me posting more code, let me know and I can add more to the question. 编辑:如果在不发布更多代码的情况下很难进行故障排除,请告诉我,我可以为问题添加更多内容。


I have two instances of basically the same code, and one of them is working while the other isn't. 我有两个基本相同代码的实例,其中一个正在工作,而另一个则没有。

Working fine: 工作正常:

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

Not working: 无法运作:

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

  $scope.toggleEditAnswer(answer);
};

I get the error: undefined is not a function at my $update function. 我收到错误: undefined不是我的$update函数中的函数。 Here's my resource: 这是我的资源:

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

What exactly am I doing wrong? 我到底在做什么错? If I understand the flow correctly, the answer.$update uses the resource somehow? 如果我正确理解流程,则answer.$update以某种方式使用资源? Also, if I use console.log to view what answer is being updated, it is the correct one: 另外,如果我使用console.log查看正在更新的answer ,那是正确的answer

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

How exactly can I troubleshoot this? 我到底该如何解决?

Could you try the following and check if it works? 您可以尝试以下方法并检查是否有效吗?

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

    $scope.toggleEditAnswer(answer);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM