角 - 处理数据正确的做法从服务器上获取? [英] Angular - correct approach to handle the data fetch from server?

查看:122
本文介绍了角 - 处理数据正确的做法从服务器上获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,我现在用的角资源 - 模块与我的 NG-应用。我不能够理解数据容易处理和规模能够方式..任何人给我/给我一个正确的方式来回答所有这些问题吗?

Assume that, I am using the angular-resource - module with my ng-app. I don't able to understand the data handling easy and scale able way.. any one give me / show me a correct way to answering all this questions?

一)一般如何在角取从URL数据?

a) Generally how to fetch data from url in the angular.?

二)情况下,每个控制器可能需要不同的URL和数据如果是如何使用过程中添加的每个控制器?

b) in case each of the controller may require different url and data if so how the fetch process added on each of controller.?

c)或需要我们做一个服务,根据控制器参数提供了数据 - ?如果是的话如何通过指标的影响,以服务

c) or need we make a service to provide the data according to the controllers parameters - if so how to pass parametes to service?

d)所有以上的有 GET PUT 删除,然后POST`如何处理所有的人 - 就是这一切需要单独的服务

d) All above have GET, PUT and DELETE, 'POST` then how to handle all them - is all this need separate services?

先谢谢了。

推荐答案

使用角资源为您服务/工厂内说。它已经提供了很多你的要求:

Use angular-resource as you said within a service/factory. It already provides a lot of your requirements:

myApp.factory("dataService", [
    "$resource",
    function ($resource) {
        return $resource("http://someBaseUrl/:action/:id", {
            id: "@id" // default parameters
        },
        {
            // custom methods
            update: { method: "PUT" },
            doOtherStuff: { method: "GET", action: "DoOtherStuff" }
        });
    }
]);

$资源默认提供了以下REST兼容功能:

The $resource default provides for the following REST compliant functions:

{ 
  'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} 
};

您有任何其他功能,包括你自己,就像更新 doOtherStuff 在上面的例子。

Any other functions you have to include yourself, like the update and the doOtherStuff in the example above.

:动作部分是一个简单的方法来提供任何自定义操作,即不符合REST

The :action part is an easy way to provide any custom action, that is not REST compliant.

在控制器用法:

myApp.controller("myCtrl", [
    "dataService",
    function (dataService) {
        // first parameter can be used for any extra query parameters
        // second parameter always is a callback
        var myData = dataService.query({}, function() {
            // success
        });

        var mySingleInstance = dataService.get({ id: 12 });

        this.doUpdate = function (entity) {
            dataService.update(entity);
            // Or, if the 'entity' is a resource entity:
            // entity.$update();
        }

        this.customMethod = function () {
            dataService.doOtherStuff();
        }
    }
]);

请参阅 https://docs.angularjs.org/api/ngResource/service/为完整的文档资源$

See https://docs.angularjs.org/api/ngResource/service/$resource for the full documentation

这篇关于角 - 处理数据正确的做法从服务器上获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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