码迷,mamicode.com
首页 > Web开发 > 详细

angular.js前端分层开发(页面和js代码分离,并将js代码分层)

时间:2018-12-28 00:48:00      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:关闭   input   use   hover   抽取   return   upd   js代码   操作   

一、

抽取模块成base.js文件
//
定义模块: var app = angular.module("eshop",[pagination]);

二、


抽取服务成brandService.js文件
/ 定义服务层:
app.service("brandService",function($http){
    this.findAll = function(){
        return $http.get("../brand/findAll.do");
    }
    
    this.findByPage = function(page,rows){
        return $http.get("../brand/findByPage.do?page="+page+"&rows="+rows);
    }
    
    this.save = function(entity){
        return $http.post("../brand/save.do",entity);
    }
    
    this.update=function(entity){
        return $http.post("../brand/update.do",entity);
    }
    
    this.findById=function(id){
        return $http.get("../brand/findById.do?id="+id);
    }
    
    this.dele = function(ids){
        return $http.get("../brand/delete.do?ids="+ids);
    }
    
    this.search = function(page,rows,searchEntity){
        return $http.post("../brand/search.do?page="+page+"&rows="+rows,searchEntity);
    }
    
    this.selectOptionList = function(){
        return $http.get("../brand/selectOptionList.do");
    }
});

三、

抽取baseController,公共js文件
app.controller("baseController",function($scope){ // 分页的配置的信息 $scope.paginationConf = { currentPage: 1, // 当前页数 totalItems: 10, // 总记录数 itemsPerPage: 10, // 每页显示多少条记录 perPageOptions: [10, 20, 30, 40, 50],// 显示多少条下拉列表 onChange: function(){ // 当页码、每页显示多少条下拉列表发生变化的时候,自动触发了 $scope.reloadList();// 重新加载列表 } }; $scope.reloadList = function(){ // $scope.findByPage($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage); $scope.search($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage); } // 定义一个数组: $scope.selectIds = []; // 更新复选框: $scope.updateSelection = function($event,id){ // 复选框选中 if($event.target.checked){ // 向数组中添加元素 $scope.selectIds.push(id); }else{ // 从数组中移除 var idx = $scope.selectIds.indexOf(id); $scope.selectIds.splice(idx,1); } } // 定义方法:获取JSON字符串中的某个key对应值的集合 $scope.jsonToString = function(jsonStr,key){ // 将字符串转成JSOn: var jsonObj = JSON.parse(jsonStr); var value = ""; for(var i=0;i<jsonObj.length;i++){ if(i>0){ value += ","; } value += jsonObj[i][key]; } return value; } });

四、

抽取业务controller成业务js文件
//
定义控制器: app.controller("brandController",function($scope,$controller,$http,brandService){ // AngularJS中的继承:伪继承 $controller(baseController,{$scope:$scope}); // 查询所有的品牌列表的方法: $scope.findAll = function(){ // 向后台发送请求: brandService.findAll().success(function(response){ $scope.list = response; }); } // 分页查询 $scope.findByPage = function(page,rows){ // 向后台发送请求获取数据: brandService.findByPage(page,rows).success(function(response){ $scope.paginationConf.totalItems = response.total; $scope.list = response.rows; }); } // 保存品牌的方法: $scope.save = function(){ // 区分是保存还是修改 var object; if($scope.entity.id != null){ // 更新 object = brandService.update($scope.entity); }else{ // 保存 object = brandService.save($scope.entity); } object.success(function(response){ // {flag:true,message:xxx} // 判断保存是否成功: if(response.flag==true){ // 保存成功 alert(response.message); $scope.reloadList(); }else{ // 保存失败 alert(response.message); } }); } // 查询一个: $scope.findById = function(id){ brandService.findById(id).success(function(response){ // {id:xx,name:yy,firstChar:zz} $scope.entity = response; }); } // 删除品牌: $scope.dele = function(){ brandService.dele($scope.selectIds).success(function(response){ // 判断保存是否成功: if(response.flag==true){ // 保存成功 // alert(response.message); $scope.reloadList(); $scope.selectIds = []; }else{ // 保存失败 alert(response.message); } }); } $scope.searchEntity={}; // 假设定义一个查询的实体:searchEntity $scope.search = function(page,rows){ // 向后台发送请求获取数据: brandService.search(page,rows,$scope.searchEntity).success(function(response){ $scope.paginationConf.totalItems = response.total; $scope.list = response.rows; }); } });

五、在页面引入js文件使用即可

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>品牌管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

    <!-- 引入angular的js -->
    <script type="text/javascript" src="../plugins/angularjs/angular.min.js"></script>
    <!-- 引入分页相关的JS和CSS -->
    <script type="text/javascript" src="../plugins/angularjs/pagination.js"></script>
    <link rel="stylesheet" href="../plugins/angularjs/pagination.css">
    
    <script type="text/javascript" src="../js/base_pagination.js"></script>
    <script type="text/javascript" src="../js/controller/baseController.js"></script>
    <script type="text/javascript" src="../js/controller/brandController.js"></script>
    <script type="text/javascript" src="../js/service/brandService.js"></script>
</head>
<body class="hold-transition skin-red sidebar-mini" ng-app="pinyougou" ng-controller="brandController">
  <!-- .box-body -->
                    <div class="box-header with-border">
                        <h3 class="box-title">品牌管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ng-click="entity={}"><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" ng-click="dele()"><i class="fa fa-trash-o"></i> 删除</button>           
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
                                      品牌名称:<input type="text" ng-model="searchEntity.name"> 品牌首字母:<input type="text" ng-model="searchEntity.firstChar"> <input class="btn btn-default" ng-click="reloadList()" type="button" value="查询">                               
                                </div>
                            </div>
                            <!--工具栏/-->

                              <!--数据列表-->
                              <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
                                  <thead>
                                      <tr>
                                          <th class="" style="padding-right:0px">
                                              <input id="selall" type="checkbox" class="icheckbox_square-blue">
                                          </th> 
                                          <th class="sorting_asc">品牌ID</th>
                                          <th class="sorting">品牌名称</th>                                          
                                          <th class="sorting">品牌首字母</th>                                                         
                                          <th class="text-center">操作</th>
                                      </tr>
                                  </thead>
                                  <tbody>
                                      <tr ng-repeat="entity in list">
                                          <td><input  type="checkbox" ng-click="updateSelection($event,entity.id)"></td>                                          
                                          <td>{{entity.id}}</td>
                                          <td>{{entity.name}}</td>                                         
                                          <td>{{entity.firstChar}}</td>                                         
                                          <td class="text-center">                                           
                                               <button type="button" class="btn bg-olive btn-xs" ng-click="findById(entity.id)" data-toggle="modal" data-target="#editModal"  >修改</button>                                           
                                          </td>
                                      </tr>
                                      
                                  </tbody>
                              </table>
                              <!--数据列表/-->                        
                              
                             
                        </div>
                        <!-- 数据表格 /-->
                        <!-- 分页 -->
                        <tm-pagination conf="paginationConf"></tm-pagination>
                        
                     </div>
                     {{selectIds}}
                    <!-- /.box-body -->
         
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">品牌编辑</h3>
        </div>
        <div class="modal-body">        
            <table class="table table-bordered table-striped"  width="800px">
                  <tr>
                      <td>品牌名称</td>
                      <td><input ng-model="entity.name" class="form-control" placeholder="品牌名称" >  </td>
                  </tr>                  
                  <tr>
                      <td>首字母</td>
                      <td><input ng-model="entity.firstChar" class="form-control" placeholder="首字母">  </td>
                  </tr>                  
             </table>                
        </div>
        <div class="modal-footer">                        
            <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" ng-click="save()">保存</button>
            <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
        </div>
      </div>
    </div>
</div>
   
</body>
</html>

 

angular.js前端分层开发(页面和js代码分离,并将js代码分层)

标签:关闭   input   use   hover   抽取   return   upd   js代码   操作   

原文地址:https://www.cnblogs.com/flgb/p/10188286.html

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