自定义服务:
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=6; $scope.pagesize = { 5 : 5, 15 : 15, 30 : 30 } //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(1,10).then(function (data) { $scope.page = data; }, function (errResponse) { console.error('Error '); } ); $scope.nextpage=function(){ $scope.page.number= ($scope.page.number+1)+1; 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 - 1); 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'); } ); }; });