升级到 angular 1.6.0 后,更新方法不再有效

Update method doesn't work anymore after upgrading to angular 1.6.0

我想使用 angularjs 中的更新函数通过 PUT 方法更新我的 json 对象,但在使用更新方法后我得到错误:

angular.js:14324 TypeError: menuFactory.getDishes(...).update is not a function
    at b.$scope.submitComment (controllers.js:104)
    at fn (eval at compile (angular.js:15152), <anonymous>:4:159)
    at e (angular.js:26673)
    at b.$eval (angular.js:17958)
    at b.$apply (angular.js:18058)
    at HTMLFormElement.<anonymous> (angular.js:26678)
    at bg (angular.js:3613)
    at HTMLFormElement.d (angular.js:3601)

这是我的 service.js 代码:

.service('menuFactory', ['$resource', 'baseURL', function($resource,baseURL) {
            this.getDishes = function(){
                return $resource("localhost:3000/"+"dishes/:id",{update:{method:'PUT' }});

                                };      
    }])

这是我的控制器代码:

 .controller('DishCommentController', ['$scope', 'menuFactory', function($scope,menuFactory) {

        $scope.mycomment= {rating:"5", comment:"", author:""};

                            $scope.dish.comments.push($scope.mycomment);

            menuFactory.getDishes().update({ id:$scope.dish.id }, $scope.dish);
    }])

为什么 angular 无法识别更新方法,我在使用 get 方法时遇到了同样的问题,但是在将我的 angular 更新到 1.6.0 版本后,get 方法的问题就解决了。但现在我对 Update 方法有同样的问题。

我做错了什么?

我想你可能错误地调用了 $resource。

Documentation 看看用法:

$resource(url, [paramDefaults], [actions], options);

您正在将操作作为 paramDefault 参数发送,因此只需为该参数输入一个空文字,除非您想对其进行自定义。

改变

$resource("localhost:3000/"+"dishes/:id",{update:{method:'PUT' }});

$resource("localhost:3000/"+"dishes/:id",{}, update:{method:'PUT' }});