Default $resource POST data

前端 未结 7 1232
天命终不由人
天命终不由人 2021-02-07 11:51

That might be strange but I need to specify some default POST data for my $resource using the factory method of the module.

Does anyone have an idea of how to do that in

7条回答
  •  醉话见心
    2021-02-07 12:28

    This is not really the angular way to do such a thing as you lose data consistency if you do it and it doesn't reflect in your model.

    Why?

    The resource factory creates the object and uses object instance data as POST. I have looked at the documentation and angular-resource.js and there doesn't seem to be a way to specify any default custom properties for the object being created by resource without modifying angular-resource.js.

    What you can do is:

    services.factory("Product", function($resource) {
        return $resource("http://someUrl", {}, {
            get   : {method: "GET", params: {productId: "-1"}},
            update: {method : "POST"}
        });
    });
    

    and in your controller:

    $scope.product = {}; // your product data initialization stuff
    $scope.product.someDataKey = 'someDataValue'; // add your default data
    
    var product = new Product($scope.product);
    product.$update();
    

提交回复
热议问题