资源ngResource(依赖ngResource模块)


<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular-resource.js"></script>
<div ng-controller="detail">
<button ng-click="query()">query</button>
<button ng-click="handle()">handle</button>
</div>
<script>
angular.module('app', ['ngResource']).factory('restful', ['$resource','$q',
function($resource,$q) {
return $resource('/:category', {
category: '@category'
},
{
query: {
method: 'get',
responseType: 'text',
interceptor: {
'response': function(response) {
return response;
},
'responseError': function(rejection) {
return $q.reject(rejection);
}
}
},
handle: {
method: 'post',
responseType: 'json',
url: "/:category/:handle",
params: {
handle: '@handle'
}
}
});
}]).controller('detail', ['$scope', 'restful',
function($scope, restful) {
$scope.query = function() {
restful.query({
category: 'a'
},
function() {})
};
$scope.handle = function() {
restful.handle({
category: 'b',
handle: 'c',
a: 'e'
},
function() {})
};
}]);
angular.bootstrap(document, ['app']);
</script>
</html>

使用factory方法创建资源,里面具体的配置的参数和上一个点滴的$http一样

  1. 当点击query方法的时候看到有一个get的请求,其中如果要给前面的category赋值,
    那么必须在默认参数上面@符号表示在调用的时候赋值
  2. 当方法里面写了url的时候会覆盖原来默认的url
  3. interceptor这个是其中多出来的参数也是一种拦截吧,
    当请求结束的时候会响应相应的事件只有responseresponseError
  4. 资源含有以下默认方法

    { 'get':{method:'GET'},
    'save':{method:'POST'},
    'query':{method:'GET',isArray:true},
    'remove':{method:'DELETE'},
    'delete':{method:'DELETE'}};
  5. 既然说了拦截那么,那些下面的方法是对所有http请求进行拦截的服务的创建,当要处理响应的事情的时候,
    就会进相应的方法体,有点类似ajaxajaxSendajaxSuccess以及ajaxError因为都是全局的,
    其中requestresponse是对象请求和响应的数据进行改写,
    需要返回修改后或者创建一个新的对象、一个promise对象也可以
    模块方法如下

    angular.module('app.interceptor', []).config(['$provide', '$httpProvider',
    function($provide, $httpProvider) {
    $provide.factory('myHttpInterceptor',
    function($q) {
    return {
    'request': function(config) {
    return config || $q.when(config);
    },
    'requestError': function(rejection) {
    return $q.reject(rejection);
    },
    'response': function(response) {
    return response || $q.when(response);
    },
    'responseError': function(rejection) {
    return $q.reject(rejection);
    }
    };
    });
    $httpProvider.interceptors.push('myHttpInterceptor');
    }]);

    或者直接使用匿名的方法

    angular.module('app.interceptor', []).config(['$httpProvider',
    function($httpProvider) {
    $httpProvider.interceptors.push(function($q) {
    return {
    'request':function(config) {
    return config || $q.when(config);},
    'requestError': function(rejection) {
    return $q.reject(rejection); },
    'response': function(response) {
    return response || $q.when(response);
    },
    'responseError': function(rejection) {
    return $q.reject(rejection);
    }
    };
    });
    }]);

过滤$filter


<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
<div ng-controller="detail">
<input type="number" ng-model="query">
<label>{{query|num:'2'}}</label>
</div>
<script>
angular.module('app', [])
.filter('num', function () {
return function (input,result) {
return input/result;
}
}).controller('detail', ['$scope','$filter',
function($scope,$filter) {
console.log($filter('num')('100','2'));
$scope.query =1
}]);
angular.bootstrap(document, ['app']);
</script>
</html>

过滤其中input参数是当前对象的值,result:??过滤名称的后面的值,
也可以在js中直接调用方法如上$filter('num')返回的就是过滤的方法


Ⅴ.AngularJS的点点滴滴-- 资源和过滤的更多相关文章

  1. Ⅵ.AngularJS的点点滴滴-- 指令

    指令 基本用法 <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angul ...

  2. Ⅳ.AngularJS的点点滴滴-- 服务

    服务(Angularjs很多方法都是服务组成的) 1.使用service方法创建的单例服务 <html> <script src="http://ajax.googleap ...

  3. Ⅶ.AngularJS的点点滴滴-- 事件

    事件(和js一样有冒泡和捕获) <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2 ...

  4. Ⅲ.AngularJS的点点滴滴-- 路由

    路由ngRoute (需要依赖ngRoute模块) <html> <script src="http://ajax.googleapis.com/ajax/libs/ang ...

  5. Ⅱ.AngularJS的点点滴滴--缓存

    模板缓存-$templateCache and 缓存工厂 $cacheFactory 1.使用script标签 <html ng-app> <script src="htt ...

  6. Ⅰ.AngularJS的点点滴滴--引导

    AngularJS已经被很多人像炒冷饭一样炒过啦,大部分都是直接复制官方文档没有说明一些注意事项,不过什么都要从头开始吧 页面引导实例化 1.自动实例化 <html> <script ...

  7. angularjs中的时间格式化过滤

    本地化日期格式化: ({{ today | date:'medium' }})Nov 19, 2015 3:57:48 PM ({{ today | date:'short' }})11/19/15  ...

  8. 【转载】AngularJS 用$sce服务来过滤HTML标签,解决无法正确显示后台传递的html标签

    angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...

  9. angularjs结合d3js实现资源展示

    转载请注明出处: 转载自Bin's Blog:  angularjs & d3 实现资源展示( http://www.wenbin.cf/post/27/ ) angularjs结合d3js实 ...

随机推荐

  1. uva 1400 - "Ray, Pass me the dishes!"

    又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...

  2. Highcharts实例

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  3. perl unload gbk oracle 数据库

    perl unload gbk Oracle 数据库 use Encode; if ( $#ARGV < 0 ){ print "请输入一个文件\n"; exit(-1); ...

  4. Redhat 安装Oracle DBI和DBD

    Redhat 安装DBI和ORACLE DBD tar -zxvf DBI-1.616.tar.gz cd DBI-1.616 perl Makefile.PL make make install 2 ...

  5. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  6. linux 下 epoll 编程

    转载自 Linux epoll模型 ,这篇文章讲的非常详细! 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显 ...

  7. 只要把鼠标移上Div方框,方框就自动顺时针旋转

    这是一个CSS3特效,IE下看不到效果.一个Div方框,在CSS3代码的作用下,只要把鼠标移上Div方框,方框就自动顺时针旋转.代码量不大,甚至有些简单,作为一个基础的CSS3实例,我想还是比较不错的 ...

  8. (转载)设计模式学习笔记(十一)——Facade外观模式

    (转载)http://www.cnblogs.com/kid-li/archive/2006/07/10/446904.html Facade外观模式,是一种结构型模式,它主要解决的问题是:组件的客户 ...

  9. C# Protect the Password inside a TextBox ZZ

    If the Text property is called, it will send an WM_GETTEXT message, so it will surely be an internal ...

  10. UVA 4728 Squares(凸包+旋转卡壳)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...