码迷,mamicode.com
首页 > 其他好文 > 详细

自定义服务与调用--------factory

时间:2018-04-18 19:12:01      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:post   update   str   users   play   passing   form data   header   lap   

自定义服务:

技术分享图片
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

 

自定义服务与调用--------factory

标签:post   update   str   users   play   passing   form data   header   lap   

原文地址:https://www.cnblogs.com/lshan/p/8876022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!