自定义服务与调用--------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里调 ...
随机推荐
- gtx860M,cuda9路1080p多高斯运动检测测试
多高斯背景差分,非常吃cpu,特别是多路视屏,所以想用gpu做检测 后面的跟踪一系列的规则判断用cpu opencv+cuda+stl做了个测试 代码: // MTTestCudaMog.cpp : ...
- $.each()与$(selector).each()
$.each()与$(selector).each()不同, 后者专用于jquery对象的遍历, 前者可用于遍历任何的集合(无论是数组或对象),如果是数组,回调函数每次传入数组的索引和对应的值(值亦可 ...
- ubuntu安装sublime教程
1.安装Sublime Text 3 及常用的神器插件 ①首先添加sublime text 3的仓库:sudo add-apt-repository ppa:webupd8team/sublime-t ...
- smarty学习——基本概念
学习一种框架,我们最基本的就是掌握框架的思想,同时了解框架的基本语法. 1.对于定界符的了解 有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的.例如,我们假定你在 ...
- 使用VBS控制声音
Set Ws = CreateObject("Wscript.Shell") Ws.Sendkeys "…" '静音 'Ws.Sendkeys "爱爱 ...
- python 之 python3内置函数
一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内 ...
- js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } ClassA ...
- centos6 安装GitLab
环境 Requirements 软件 版本CentOS 6.6Python 2.6Ruby 2.1.5Git 1.7.10+Redis 2.0+MySQL GitLab 7-8-stableGitLa ...
- import和export语法报错
“最近在学习ES6”,但是在chrome中新建了js通过ES6语法(import,export)无法引入外部JS,报错: Uncaught SyntaxError:Unexpected token { ...
- elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(二)动态添加任务需求
之前一篇用过了如何在使用创建最简单的任务:比如每天定时清空系统的缓存 这篇文章主要讲解:如何运用elastic-job-lite做灵活的细粒度任务,比如: 如何定时取消某个订单在下订单后30分钟未支付 ...