zoukankan      html  css  js  c++  java
  • 自定义服务与调用--------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;
        }
        
    }]);
    View Code

    服务调用:  引入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;
            }
            
      });
    View Code
    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');
                      }  );
          
          
          
      };
      });
    View Code
  • 相关阅读:
    android闹钟小案例之知识点总结
    转载---程序员发展之路
    android闹钟小案例之功能阐述
    基础篇:2.熟练掌握相关监听器的注册和使用
    基础篇:1.掌握基本组件和容器组件的使用
    微博分享、注销功能的实现
    新浪微博授权认证的实现
    新浪微博分享功能的简单实现
    读《编程高手箴言》笔记一
    Spark内核架构核心组件.txt
  • 原文地址:https://www.cnblogs.com/lshan/p/8876022.html
Copyright © 2011-2022 走看看