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 ...
随机推荐
- ThinkPHP 3.1.2 CURD特性 -3
一.ThinkPHP 3 的CURD介绍 (了解) 二.ThinkPHP 3 读取数据 (重点) 对数据的读取 Read $m=new Model('User'); $m=M('User'); ...
- 在linux下使用am335x的DMA
[本文首发于cnblogs,作者:byeyear,Email:east3@163.com] 这几天在弄am3358的DMA,简化应用环境如下: am3358的FSMC接了一片FPGA: FPGA上接A ...
- 阿里云ECS安装flannel启动问题
在阿里云ECS安装flannel,安装过程可以在网上找文章,这样的文章很多.我这里讲一下启动flannel遇到的两个问题的解决方法. 1,network.go:102] failed to retri ...
- php的语法
常量:不变的量: php 设置常量的方法:define()方法: define("常量名","常量的值",true) 参数三:大小写是否敏感: php中的常量, ...
- [UE4]工程设置:自动捕获鼠标、通过代码设置鼠标显示隐藏、输入模式、编译时自动保存
一.在4.20版本中运行游戏,在没有进行任何设置的情况下,游戏不会自动捕获鼠标,游戏不会接受输入,需要手动点一下游戏界面才行.如果要跟老版本一样运行游戏自动捕获鼠标,需要进行设置 二.也可以通过代码的 ...
- 使用 keepalived 设置虚拟 IP 环境(转载)
使用 keepalived 设置虚拟 IP 环境 原文地址:https://blog.csdn.net/kongxx/article/details/73173762 使用 keepalived 设置 ...
- http、TCP/IP协议与socket之间的区别(转载)
http.TCP/IP协议与socket之间的区别 https://www.cnblogs.com/iOS-mt/p/4264675.html http.TCP/IP协议与socket之间的区别 ...
- Java基础知识_毕向东_Java基础视频教程笔记(14-18集合框架)
14天-01-集合框架集合类出现:面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式.数组与集合类同是容器,有何不同? 数组长度 ...
- 03-String常用方法
1.获取方法 /* * 编辑:刘诗华 int length() 获取字符串的长度 char charAt(int index) 获取特定位置的字符 (角标越界) int indexOf(String ...
- 给 Windows 文件菜单添加 "用XX程序打开" "用XX编辑" "用XX运行"
有什么用就不用多说了,这可是个很有用的技巧.可以创造自己的文件格式,也可以给已有的文件添加多种打开方式 在注册表[HKEY_CLASSES_ROOT]下找到或者建立对应的扩展名 如果想对所有文件都生效 ...