自定义服务与调用--------factory
自定义服务:
angular.module('myApp').factory('UserService',['$http','$q',function ($http,$q) {
// 定义一个方法工厂
var factory = {
httpGet : httpGet,
login:login
};
return factory;
function httpGet(no,size) {
var deferred = $q.defer();
$http.get("/user/list.action?pageSize="+size+"&pageNo="+no)
.then(function (response) {
deferred.resolve(response.data);
},
function (errResponse) {
console.error("Error !!")
deferred.reject(errResponse);
}
);
return deferred.promise;
}
function login(params) {
var deferred = $q.defer();
$http({
method : 'POST',
url : '/user/login.action',
data : $.param(params), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } , // set the headers so angular passing info as form data (not request payload)
dataType:'json'
})
.then(function (response) {
deferred.resolve(response.data);
},
function (errResponse) {
console.error("Error !!")
deferred.reject(errResponse);
}
);
return deferred.promise;
}
}]);
服务调用: 引入js
var app = angular.module('myApp', []);
app.controller('userCtrl', function ($scope, $http,UserService) {
/******************paging************pageSize******/
$scope.pageSize=;
$scope.pagesize = {
: ,
: ,
:
}
//httpGet(2,8);
$scope.selectchange=function(){
//debugger
UserService.httpGet($scope.page.number,$scope.pageSize).then(function (d) {
$scope.page = d;
} , function (errResponse) {
console.error('Error');
} );
}
/**
* page laod data
*/
UserService.httpGet(,).then(function (data) {
$scope.page = data;
}, function (errResponse) {
console.error('Error ');
}
);
$scope.nextpage=function(){
$scope.page.number= ($scope.page.number+)+;
if($scope.page.number>=$scope.page.totalPages){
$scope.page.number=$scope.page.totalPages;
}
UserService.httpGet($scope.page.number,$scope.pageSize).then(function (d) {
$scope.page = d;
} , function (errResponse) {
console.error('Error');
} );
}
$scope.prepage=function(){
$scope.page.number= ($scope.page.number - );
UserService.httpGet($scope.page.number,$scope.pageSize).then(function (d) {
$scope.page = d;
} , function (errResponse) {
console.error('Error');
} );
}
/*********** ******delete********* ***********/
$scope.deleteuser=function($event,id){
//debugger;
var flag = confirm("Are you sure delete this user ?");
if(flag){
var url = $($event.target).attr("href1")+id;
$http.get(url)
.then(function (response) {
//alert("aa");
if(response.data.status="ok"){
location.reload() ;
alert("delete success !");
}else{
alert("delete fail !");
}
});
}
return false;
}
/*********** ******update********* ***********/
$scope.update=function($event,id){
location.href="/user/toupdate.action?id="+id;
}
});
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope ,$http,$location ,UserService) {
$scope.login = function () {
// // alert('user is saved');
// $http({
// method : 'POST',
// url : '/user/login.action',
// data : $.param($scope.user), // pass in data as strings
// headers : { 'Content-Type': 'application/x-www-form-urlencoded' } , // set the headers so angular passing info as form data (not request payload)
// dataType:'json'
// })
// .success(function(data) {
// //alert(data.status);
//
// if (data.status=="ok") { //login success
// window.location.href="/user/tolist.action";
// }
// if (data.status=="no") { //no such user
// $scope.message="this user is not exist !";
// // window.location.href="/user/toLogin.action";
// }
// if (data.status=="pno") { //password wrong
// $scope.message1="password wrong !";
// // window.location.href="/user/toLogin.action";
// }
// });
UserService.login($scope.user).then(function (data) {
// console.log(d);
if (data.status=="ok") { //login success
window.location.href="/user/tolist.action";
}
if (data.status=="no") { //no such user
$scope.message="this user is not exist !";
// window.location.href="/user/toLogin.action";
}
if (data.status=="pno") { //password wrong
$scope.message1="password wrong !";
// window.location.href="/user/toLogin.action";
}
} , function (errResponse) {
console.error('Error');
} );
};
});
自定义服务与调用--------factory的更多相关文章
- angularJs 自定义服务 provide 与 factory 的区别
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...
- 【AngularJS中的自定义服务service VS factory VS provider】---它们的区别,你知道么?
在介绍AngularJS自定义服务之前,我们先来了解一下AngularJS~ 学过HTML的人都知道,HTML是一门很好的伪静态文本展示设计的声明式语言,但是,要构建WEB应用的话它就显得乏力了. 而 ...
- 深究AngularJS——自定义服务详解(factory、service、provider)
前言 3种创建自定义服务的方式. Factory Service Provider 大家应该知道,AngularJS是后台人员在工作之余发明的,他主要应用了后台早就存在的分层思想.所以我们得了解下分 ...
- ng自定义服务(利用factory)
ng中我们可以自己定义自己的服务,服务中是一些使用重复率较高的方法.因此有效的使用服务可以提高开发速度. ng中定义服务的方法有多种,service,factory,provide,在此我只介绍最长用 ...
- angularjs factory,service,provider 自定义服务的不同
angularjs框架学了有一段时间了,感觉很好用.可以把angularjs的app理解成php的class,controller是控制器,而内置服务和自定义服务就可以理解成models了.angul ...
- Angular.js之服务与自定义服务学习笔记
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)
刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...
- angular factory Services provider 自定义服务 工厂
转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1602.html 1.在app.js 中声明了模块的依赖 var phonecatApp = angular ...
- angularjs 自定义服务(serive,factory,provder) 以及三者的区别
1.Serive 服务:通过service方式创建自定义服务,相当于new的一个对象:var s = new myService();,只要把属性和方法添加到this上才可以在controller里调 ...
随机推荐
- 【转载】Python字符串操作之字符串分割与组合
1. str.split():字符串分割函数 通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. 语法: str.split(s, num)[n] 参数说明: s:表示指定的分隔符,不写的话, ...
- hdu1224 dp(dp + 栈/父亲数组记录路径)
题意:给定 n 个城市的有趣度,并给出可以从那些城市飞到那些城市.其中第一个城市即起始城市同样也作为终点城市,有趣度为 0,旅行途中只允许按有趣度从低到高旅行,问旅行的有趣度最大是多少,并输出旅行路径 ...
- HTML第二课——css
请关注公众号:自动化测试实战 先给大家提个建议,就是用sublime编辑器来编写.用其他的也无所谓,我只是建议,因为这个会帮你自动补全很多代码. css概念 css叫层叠样式表.意思就是一层一层的叠加 ...
- SQLite数据库学习小结——Frameworks层实现
3. SQLite的Frameworks层实现 3.1 Frameworks层架构 Android系统方便应用使用,在Frameworks层中封装了一套Content框架,之所以叫Content框架而 ...
- 【转】每天一个linux命令(28):tar命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/30/2795656.html 通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候 ...
- 【转】每天一个linux命令(27):linux chmod命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/29/2794010.html chmod命令用于改变linux系统文件或目录的访问权限.用它控制文 ...
- Vue 的自定义事件系统:实现子组件跟父组件通信
父组件可以在使用子组件的地方直接用 v-on 来监听子组件触发的事件 v-on 写在引用的子组件上, 语句 this.$emit('xxx') ,写在子组件里.
- mac系统下 Homebrew 使用
brew 又叫 Homebrew,是一款Mac OS平台下的软件包管理工具. brew 常用命令: 命令 作用 brew install [package] 安装包 brew uninstall [p ...
- centos7 安装Zabbix3.0
1 安装Mariadb数据库(代替MySQL)yum -y install mariadb*systemctl start mariadbsystemctl enable mariadb #自启动 2 ...
- Linux Home目录硬盘空间缩减
Linux Home目录硬盘空间缩减 操作 基于centos6.5 x86_64, runlevel 3,命令行模式,测试成功. 1.首先查看磁盘使用情况 [root@localhost ~]# ...