angular.js前端分层开发(页面和js代码分离,并将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: , // 当前页数
totalItems: , // 总记录数
itemsPerPage: , // 每页显示多少条记录
perPageOptions: [, , , , ],// 显示多少条下拉列表
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,);
} } // 定义方法:获取JSON字符串中的某个key对应值的集合
$scope.jsonToString = function(jsonStr,key){
// 将字符串转成JSOn:
var jsonObj = JSON.parse(jsonStr); var value = "";
for(var i=;i<jsonObj.length;i++){ if(i>){
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代码分层)的更多相关文章
- js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到
js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到 2.图2的内容为directionkey.js的内容
- 配合sublime使用flexible.js实现微信开发页面自适应
什么是flexible.js 是一个终端设备适配的解决方案.也就是说它可以让你在不同的终端设备中实现页面适配. 是一个用来适配移动端的javascript框架.根据宽度的不同设置不同的字体大小,样式间 ...
- 81.node.js前端html时页面格式错乱解决办法
var http = require("http"); var url = require("url"); var fs = require("fs& ...
- JS前端图形化插件之利器Gojs组件(php中文网)
JS前端图形化插件之利器Gojs组件(php中文网) 一.总结 一句话总结:php中文网我可以好好走一波 二.JS前端图形化插件之利器Gojs组件 参考: JS前端图形化插件之利器Gojs组件-js教 ...
- Node.js 从零开发 web server博客项目[express重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[安全]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- JS前端无侵入实现防止重复提交请求技术
JS前端无侵入实现防止重复提交请求技术 最近在代码发布测试的过程中,我发现有些请求非常的消耗服务器资源,而系统测试人员因为响应太慢而不停的点击请求.我是很看不惯系统存在不顺眼的问题,做事喜欢精益求精, ...
- Node.js 从零开发 web server博客项目[日志]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[登录]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[项目介绍]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
随机推荐
- synchronized基础
synchronized 例子 例1,没有同步的时候运行同一个对象的同一个方法的结果: public class TestSyn { public void showMsg() { try { for ...
- IDEA创建Springmvc项目
项目主要步骤如下: 1.创建一个javaweb动态项目 2.导入springmvc demo所需要的jar包 3.生成项目war包 4.配置项目tomacat服务器 5.配置web.xml文件 6.编 ...
- 【mysql】索引优化记录
基础知识 Innodb存储引擎 支持行锁 支持事务: Myisam存储引擎 只支持表锁: 不支持事务: 常见索引列表 独立的列 前缀索引(索引选择性) 多列索引(并不是多个单列索引,索引顺序很重要) ...
- go get golang.org/x 包失败解决方法
由于墙的原因,国内使用 go get安装golang 官方包可能会失败 解决方法 方法1 [不需要FQ] Win10下相关配置: GOPATH : E:\go 安装记录: E:\>go get ...
- mina2的processor
processor顾名思义,就是进行IO处理,处理当前session的数据读写,并进行业务处理. 在mina server初始化的时候,会初始化一个processor池,通过NioSocketAcce ...
- Python——numpy(python programming)
np.insert(a,第几行/列,数,axis=??) sum,mean,std,var,min,max,argmin,argmax,unique np.random a=np.random.nor ...
- 计划任务at、crontab
at一次性计划任务 格式: at + 时间 命令 安装at # yum install at -y 如果执行at命令时,出现一下情况 Can't open /var/run/atd.pid to si ...
- [UE4]删除UI:Remove from Parent
同时要将保存UI的变量清空,以释放占用的系统内存
- svn 的add 和 commit
add 功能:向文件拷贝所在的文件夹中添加新的文件,并作出标识,是新添加的,下一步提交时将一并提交到Subversion版本库中去.简单的说就是将一新文件加入svn,你添加再提交后该文件就进入subv ...
- Linux下安装与卸载anaconda
安装:到安装文件夹的目录下输入 bash Anaconda3-4.1.1-Linux-x86_64.sh 卸载:输入