angular.js前端分層開發(頁面和js程式碼分離,並將js程式碼分層)
阿新 • • 發佈:2018-12-28
一、
抽取模組成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>