AngularJS学习笔记--$resource的应用

浅谈AngularJS中使用$resource(已更新) 主要介绍了浅谈AngularJS中使用$resource(已更新)的相关资料,需要的朋友可以参考下 立即下载

AngularJS学习笔记–$resource的应用

$resource对$http进行了封装,通过$resource将资源映射为对象,我们可以像操作对象一样方便的调用RESTful的服务端数据源。下面是一个$resource的实例:

使用$resource服务需要导入相应的js文件:

<script type="text/javascript" src="framework/angular-1.3.0.14/angular.min.js"></script>
<script type="text/javascript" src="framework/angular-1.3.0.14/angular-resource.min.js"></script>

使用$resource创建一个资源对象:

    var studentResource = $resource(
        '/rest/student/:id', /*参数一:服务端api的url*/
        {},/*参数二:默认的请求参数*/
        {
            /*参数三:声明可执行的actions*/
            get: {method: 'GET'},
            save: {method: 'POST'}
        }
    );

声明调用资源对象的方法:

    var getSutudent = function(studentId){
        var deferred = $q.defer();
        studentResource.get(
            {id: 100}, //参数
            function(student){ //请求成功回调函数
                deffered.resolve(student);
            },
            function(error){ //请求出错回调函数
                //log
                deferred.reject(error);
            }
        );
        return deferred.promise;
    };

    var submitStudent = function(student){
        var deferred = $q.defer();
        studentResource.save(
            {id: student.id},
            {
                student: student //构造POST请求的数据
            },
            function(result){ //请求成功回调函数
                deffered.resolve(result);
            },
            function(error){ //请求出错回调函数
                //log
                deferred.reject(error);
            }
        );
        return deferred.promise;
    };

调用声明的方法获取、修改资源:

    var student;
    var deferred = $q.defer();
    getSurvey(studentId).then(
        function (student) {
            deferred.resolve(student);
        }, 
        function (error) {
            deferred.reject(error);
        }
    );
    student = deferred.promise;
qt使用qxlsx实现xlsx、xls表格文件快速写入和读取 新年第二篇文章,收尾一个去年计划中发表文章和实现功能。 阅读详情

相关推荐

Allegro Design Entry CIS 与 OrCAD Capture CIS:PCB设计工具链中的双雄对决

本文深入对比了Cadence旗下两大原理图设计工具Allegro Design Entry CIS与OrCAD Capture CIS。Allegro Design Entry CIS源自Concept HDL,定位高端,强调自顶向下的层次化设计和与Allegro PCB Editor的无缝约束管理,适合大型复杂系统及团队协作。OrCAD Capture CIS则以其出色的易用性、广泛的兼容性和强大的CIS数据库功能著称,是中小型项目及跨工具链协作的通用选择。文章从历史渊源、核心功能、适用场景等多维度剖析,

bush的博客 487

$resource 数据交互插件

学习要点:1. $resource 简介2. $resource 的使用3. 何时可以使用 Angular 资源(选看)4. Angularjs 自定义指令(后期讲)1. $resource 简介$resource:$resource 服务的核心价值在于:提供给开发者与 RESTful 风格 WebServices 交互的更好的用户体验,它封装了较为低级的$http,这样就不...

weixin_34239592的博客 159

5分钟搞定PaddleOCR PP-OCRv5环境搭建:从安装到第一个识别Demo

本文提供了一份详细的PaddleOCR PP-OCRv5环境搭建与快速入门指南。通过清晰的步骤,指导读者在5分钟内完成从创建Python虚拟环境、安装PaddlePaddle框架和PaddleOCR库,到编写并运行第一个OCR识别Demo的全过程,帮助开发者快速体验最新的PP-OCRv5模型的文字识别能力。

sprite的博客 1267

angular $resource模块

目录(?)[-] 安装 应用resource 扩展resource 上一篇中讲到使用$http同服务器进行通信,但是功能上比较简单,angularjs还提供了另外一个可选的服务$resource,使用它可以非常方便的同支持restful的服务单进行数据交互。 安装 ngResource模块是一个可选的angularjs模块,如果需要使用,我们要单独引用...

weixin_30289831的博客 185

手把手教你用DLP4500投影仪玩转结构光3D扫描(附固件烧录与序列设置避坑指南)

本文详细解析了如何使用DLP4500投影仪搭建结构光3D扫描系统,涵盖硬件组装、固件烧录、图案生成与优化等全流程。重点解决了固件烧录失败、图像格式错误等常见问题,并提供了实用的避坑指南和进阶技巧,帮助读者快速掌握DLP4500在3D扫描中的应用。

weixin_42662293的博客 301

C++中const和指针*的组合问题

今天在复习> 章节7.3的时候想到以前做的一个题,const和指针运算符*的若干种组合的合法性和意义问题,有点混淆,所以写了一个测试代码来验证一下,简单明了,一目了然。如果有什么遗漏的话欢迎指出。 首先是在Ubuntu14.04.1下用gcc 4.8.4编译运行的结果,编译器报错信息都附加在对应语句后面的注释里 /* * Note : Test for the combinations of

theColdOne的专栏 1081

白话$resource,$resource中的增删改查

前言  $resource详解,在学习angular的过程中,我们已经知道,$http能十分便捷的为我们实现与后端的数据交互,格式如下: $http({method:'GET'},url:'XX').then(successFun,errorFcun)  选好交互方式(method是GET或者POST),填好地址(url是你要交互的后台地址),然后在成功(successFun)以及失...

weixin_30470857的博客 248

基于MATLAB的特征模式分解(FMD)算法详解

本文详细介绍了基于MATLAB的特征模式分解(FMD)算法实现。FMD是一种新型自适应信号分解方法,通过自适应FIR滤波器组和相关峭度优化目标,有效解决传统方法(如EMD、VMD)的模态混叠问题。文章系统阐述了FMD的算法原理、数学模型和实现步骤,包括自适应FIR滤波器设计、相关峭度优化和模态选择机制。与现有方法相比,FMD在旋转机械故障诊断中展现出更强的特征提取能力和噪声鲁棒性。MATLAB实现部分包含核心函数、参数优化和可视化工具,并通过轴承故障等案例验证了算法的有效性。

qq_42212808的博客 192

Angular之$resource服务

1. $resource服务介绍   $http服务提供的实现极为简单和低级,可以用来发送XHR请求,同时它还为你提供了很大的可控性和灵活性。但是,在大多数情况下,我们需要处理对象,以及封装了特定属性和方法的对象模型,例如一个person对象(带有详细信息),或者一个信用卡对象。    $resource就是为这一功能而设计的。AngularJS中的resource(资源)允许我们用描述性

qq_27467601的博客 552

4、接收天线:接收电压、功率和传输系数

本文详细探讨了接收天线的性能参数,包括接收电压、功率和传输系数,通过建立等效电路模型分析接收偶极子的电流和开路电压,并定义电压和功率传输函数。研究揭示了天线长度、工作频率及极化失配对传输性能的影响,并提出了优化接收天线性能的策略,为天线系统的设计与优化提供了理论支持。

ll5678的博客 233

【C#程序员入门AI】必知的一些知识

2026年C#调用AI,核心就4个概念+3种方式4个概念:大模型(大脑)、嵌入(数字密码)、向量(载体)、Agent(超级助理)3种方式:云端API(快)、本地模型(隐私)、混合部署(最佳)不用怕复杂,跟着这个思路走,你就能从“AI小白”变成“C# AI开发能手”,把AI集成到你的.NET项目里,提升效率、创造价值!

人工智能AI技术 410

$resource

1.$resource是一个创建资源对象的工厂,用来创建同服务端交互的对象。 例如: var User = $resource('/api/users/:userId', {userId:'@id'}); 返回的User对象包含了同后端服务进行交互的方法,我们可以把User对象理解成同RESTFul的后端服务进行交互的接口。 转载于:https://www.cnblogs.com...

aiweisheng6193的博客 175

基于simulink的AI融合与深度学习控制的场景实例:基于强化学习的电动汽车能量管理策略优化仿真

本文介绍了基于强化学习的电动汽车能量管理策略优化仿真方法。通过Simulink构建包含驾驶循环输入、电池子系统、电机驱动系统和强化学习控制器的仿真模型,采用DQN等算法训练智能代理,设计奖励函数优化能量分配。详细说明了建模步骤和参数设置,包括环境定义、代理训练和性能评估指标。该方法能有效提升电动汽车的续航里程、电池寿命和能源利用效率,为智能能量管理系统设计提供参考。

xiaoheshang_123的博客 154

AngularJS的$resource

$http $http服务是基于$q服务的,提供了promise封装,它接受一个配置对象参数,并返回一个promise对象。同时,它还提供了2个方法用来定义Promise回调:success 和 error。 $http({method:"GET", url:"/someUrl"}). success(function(data, status, headers, config){

2万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

AngularJS中的$resource使用与Restful资源交互

1.AngularJS中的 $resource  这个服务可以创建一个资源对象,我们可以用它非常方便地同支持RESTful的服务端数据源进行交互,当同支持RESTful的数据模型一起工作时,它就派上用场了。       REST是Representational State Transfer(表征状态转移)的缩写,是服务器用来智能化地提供数据服务的一种方式            1

逍遥飞鹤的专栏 1万+

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

angularjs 之 $resource

这个服务可以创建一个资源对象,可以方便同RESTFUL的服务端数据源进行交互,当同支持RESTful的数据模型一起工作时,他就派上用场了。 REST是representational state transfer(表征状态转移)是服务器智能化地提供数据服务的一种方式 1)我们首先需要引...

213

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

angularJS $resource

$resource是一个更方便地与RESTful服务器API进行交互,可以方便地定义一个REST资源,而不必手动所有的声明CRUD方法的Service。 使用 1.要使用$resource首先要在HTML中引入它的独立的js文件; <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/...

aizhanqian5077的博客 217

AngularJs(四):$Http,$resource

一.使用$http $http服务是只能接受一个参数的函数,这个参数是一个对象,包含了用来生成HTTP请求的配置内容。这个函数返回一个promise对象,具有success和error两个方法。例如代码: $http({    method:"GET",    url:'/xx/xx' }).success(function(data,status,headers,config)

Allen 6369

angularjs中$resource的使用

一、使用$resource    1、引入 angular-resource.min.js     2、定义模块时加载 ngResource       var app = angular.module('app',["ngResource"]); 二、支持的方法 支持方法 :  { 'get': {method:'GET'}, 'save':

lzyisaking的博客 2888

AngularJs $resource 高大上的数据交互

$resource 创建一个resource对象的工厂函数,可以让你安全的和RESFUL服务端进行数据交互。 需要注入 ngResource 模块。angular-resource[.min].js 默认情况下,末尾斜杠(可以引起后端服务器不期望出现的行为)将从计算后的URL中剥离。 这个可以通过$resourceProvider配置: app.config(["$resourc

yzbben的博客 1680

angularjs请求$resource的5种基本操作方法

angularjs$resource的5种基本操作方法 angular请求资源有五种基本方法,分别是get,save,query,delete,remove 【HTTP GET类型的方法】 ①GET请求: get(params,successFn,errrorFn) 不定义具体的参数,get()请求通常被用来获取单个资源。 //GET let $com = $resource("api/user/getById.do"); $com.get(function(res){ //处

code_carrierV1的博客 1441

AngularJs错误:Error: [$resource:badcfg] Error in resource configuration

AngularJs错误:Error: [$resource:badcfg] Error in resource configuration 错误:[$Resource:badcfg]在资源配置中的错误。 预计响应包含一个数组,但得到了一个对象

nightmare 5770

AngularJS $resource RESTful example

我想用$resource来叫我的REST风格的Web服务,(我还在工作),但我想看看,如果我得到了我的AngularJS脚本正确的第一。 该待办事项DTO有: {id, order, content, done} :cmd 是这样我就可以打电话api/1/todo/reset清除数据库中的待办事项表。 这里是我的理解注释的代码: function TodoService($

大爱无疆的博客 3889

angularjs ajax header,AngularJS $resource Headers POST

I would like to know how to pass headers to AngularJS $resource methodHere is the factory method.factory('DataRepository', function ($resource) {return $resource(serviceUrlPrefix + '/api/v1/AppList/:i...

weixin_42311349的博客 230

cms php angularjs,浅谈AngularJS中使用$resource(已更新)

这个服务可以创建一个资源对象,我们可以用它非常方便地同支持RESTful的服务端数据源进行交互,当同支持RESTful的数据模型一起工作时,它就派上用场了。REST是Representational State Transfer(表征状态转移)的缩写,是服务器用来智能化地提供数据服务的一种方式1)我们首先需要引入ng-Resource 模块,在angular之后2) 在我们的应用中需要将其当做依...

weixin_39540834的博客 143
上一篇: AngularJS学习笔记--服务的创建方法factory() service() provider()等
下一篇: AngularJS学习笔记--使用angular.extend()实现工厂模式
菠萝面包
博客等级 码龄11年 0粉丝 4原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值