【问题标题】:ngResource update method confusionngResource 更新方法混乱
【发布时间】:2016-03-29 08:36:45
【问题描述】:

我查看了有关更新方法使用的各种问题的各种教程和答案,但到目前为止,我发现没有一个对我有用。

我的服务如下所示:

angular.module("testApp").service("PersonData", function($resource) {
    return $resource("api/persons/:id", {
        id: "@_id"
    }, {
        update: {
            method: "PUT"
        }
    });
});

我在我的控制器中尝试了各种方法,例如:

PersonData.get({id:id})
    .$promise.then(function(result) {
      result.name = "new";
      result.$update();
    });

  var x = new PersonData({id: id});
          x.name = "new";

          x.update();

每次我得到一个错误,上面写着update is not a function。我该如何解决这个问题?

【问题讨论】:

    标签: angularjs ngresource


    【解决方案1】:

    我没有尝试下面的示例代码,但一个明显的问题是您需要将依赖注入 $resource 作为服务的参数 ['$resource']

    试试

     angular.module("testApp").service("PersonData", ['$resource', function($resource) {
         return $resource("api/persons/:id", {
             id: "@_id"
         }, {
             update: {
                 url: 'url for update path', //an option
                 method: "PUT",
                 params: {
                     id: "@_id"
                 }
             }
         });
     }]);
    

    然后:在控制器中调用 PersonData 服务:

    var invoked = PersonData.update({
             id:: myID
         })
         .$promise.then(function(data) {
            // do something with a callback here if needed.
            // (Like an alert or another update, etc.) 
         });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多