默认$资源POST数据 [英] Default $resource POST data

查看:194
本文介绍了默认$资源POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是陌生的,但我需要使用模块的工厂方法来指定我的$资源一些默认的POST数据。

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

有没有人对如何做,在AngularJS的想法?

Does anyone have an idea of how to do that in AngularJS ?

编辑:

好吧,我想要做的是这样的:

Well, i want to do something like this :

/**
 * Module declaration.
 * @type {Object}
 */
var services = angular.module("services", ["ngResource"]);

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

在哪里数据是对我的未来POST请求的默认数据。

Where data is the default data for my future POST requests.

推荐答案

这是不是真的为你失去数据的一致性,如果你这样做,它不会在你的模型反映做这样的事情的角度的方式。

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.

为什么?

资源工厂创建对象和使用对象实例的数据作为POST。我已经看了看文档和角resource.js和似乎没有被指定,而无需修改角resource.js由资源正在创建的对象的默认自定义属性的一种方式。

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.

你可以做的是:

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

和在你的控制器:

$scope.product = {}; // your product data initialization stuff
$scope.product.someDataKey = 'someDataValue'; // add your default data

var product = new Product($scope.product);
product.$update();

这篇关于默认$资源POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆