迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > Angular >

AngularJS $resource

作者:迹忆客 最近更新:2023/04/12 浏览次数:

AngularJS $resource 是一项服务,可让您以对 Angular 更友好的方式访问 RESTful Web 服务。 $resource 负责序列化和反序列化数据,让您专注于应用程序逻辑。

此外,AngularJS $resource 提供了与 RESTful API 通信的功能。 这些函数简化了从各种来源检索数据的过程,支持所有 HTTP 方法。

默认情况下,$resource 执行以下五个 REST 操作。

  1. 获取项目列表
  2. 获取项目
  3. 插入项目
  4. 更新项目
  5. 删除项目

AngularJS $resource 与 $http

$resource 服务是一种查询远程资源(如 REST 端点)的方法。 它是一个 AngularJS 服务,提供创建代表远程数据的对象和对这些对象执行 CRUD 操作的功能。

$http 是一个 AngularJS 服务,提供向服务器发出 AJAX 请求的功能。 它可以与任何 AJAX 库一起使用,但必须与 AngularJS 的 $resource 服务一起使用才能获得完整的 CRUD 功能。

$resource 是一个声明一次的变量,然后用于在我们的控制器/服务中调用 get、post、query、remove 和 delete。 $resource 包含所有方便的内置方法,这是一个很棒的功能。

我们在使用$resource访问REST API时不需要写重复的代码,这让我们的调用更肤浅,更相关。


AngularJS $resource 的例子

JavaScript 代码:

var app = angular.module('plunker', ['ngResource']);
app.factory('demo', function($resource) {

  var url = 'sample.json';
  var res = $resource(url, null, {
    query: {
        isArray: true,
      transformResponse: function(data) {
        var items = angular.fromJson(data);
        var models = [];

            class Person {
            constructor(config) {
           this.name=config.name;
               }
             sayHello() {
               }                        }
             angular.forEach(items, function(item) {
          models.push(new Person(item));
        });
        console.log("models: ", models);
        return models;
      }
    }
  });
  return res;
});

app.controller('MainCtrl', function($scope, demo) {
  demo.query()
    .$promise.then(function (data) {
      $scope.names = data;

    });
});

HTML 代码:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14"></script>
    <script data-require="angular-resource@1.3.x" data-semver="1.3.14" src="http://code.angularjs.org/1.3.14/angular-resource.js"></script>

    <script src="app.js"></script>
  </head>
<body ng-controller="MainCtrl">
  Hello.
  <ul>
    <li ng-repeat="n in names">
      {{n.name}}
    </li>
  </ul>
</body>
</html>

它对开发人员来说是一个强大而有用的工具,简化了从服务器查询数据的过程。

它最常用于 RESTful API 上的 CRUD 操作,但也可用于从任何 API 检索数据。

AngularJS $resource 的主要好处是它通过减少编写样板代码的需要来节省时间并使编码更容易。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便