一、

抽取模块成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代码分层)的更多相关文章

  1. js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到

    js文件需要jsp页面中的div时,此js文件必须在div之后才能获得值,否则获取不到 2.图2的内容为directionkey.js的内容

  2. 配合sublime使用flexible.js实现微信开发页面自适应

    什么是flexible.js 是一个终端设备适配的解决方案.也就是说它可以让你在不同的终端设备中实现页面适配. 是一个用来适配移动端的javascript框架.根据宽度的不同设置不同的字体大小,样式间 ...

  3. 81.node.js前端html时页面格式错乱解决办法

    var http = require("http"); var url = require("url"); var fs = require("fs& ...

  4. JS前端图形化插件之利器Gojs组件(php中文网)

    JS前端图形化插件之利器Gojs组件(php中文网) 一.总结 一句话总结:php中文网我可以好好走一波 二.JS前端图形化插件之利器Gojs组件 参考: JS前端图形化插件之利器Gojs组件-js教 ...

  5. Node.js 从零开发 web server博客项目[express重构博客项目]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  6. Node.js 从零开发 web server博客项目[安全]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  7. JS前端无侵入实现防止重复提交请求技术

    JS前端无侵入实现防止重复提交请求技术 最近在代码发布测试的过程中,我发现有些请求非常的消耗服务器资源,而系统测试人员因为响应太慢而不停的点击请求.我是很看不惯系统存在不顺眼的问题,做事喜欢精益求精, ...

  8. Node.js 从零开发 web server博客项目[日志]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  9. Node.js 从零开发 web server博客项目[登录]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

  10. Node.js 从零开发 web server博客项目[项目介绍]

    web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...

随机推荐

  1. js页面滚动时层智能浮动定位实现

    直接上代码 $.fn.smartFloat = function (className) { var position = function (element) { var top = element ...

  2. WAP用户评论简单实现瀑布流加载

    wap端经常会遇到产品或者评论的加载,但是分页的体验不是很好,所以选择通过js实现瀑布流加载方式. 第一步:需要动态加载的主要内容,这里需要动态加载的是li标签的内容 <ul class=&qu ...

  3. maven不同环境的profile配置

    1.开发的时候经常需要加载不同的环境,比如本地开发环境dev,生产环境product.如果需要手动去修改的话就太麻烦了,自己实现了maven资源替换,然后多环境下的配置文件管理的demo,在此贴出来. ...

  4. FTP的应用

    创建共享文件,并删除 cd /var/ftp/---------------------(切换到ftp目录下) mkdir aaaaa-------------------(创建aaaaa目录) ll ...

  5. [UE4]Tool Tip - 提示信息

    一.每一个Widget都有Tool Tip,在运行时鼠标移动到UI上,就会显示填写的Tool Tip文字   二.Toop Tips的字体样式和大小不可更改.但是可以Tool Tip可以绑定到一个Wi ...

  6. [UE4]圆形小地图

    一.创建一个名为M_RoundRetainer的材质 二.创建一个名为RoundMiniMap的UserWidget 三.TestMiniMap中将添加进来 四.运行游戏

  7. SpringMVC 源码分析

    一个东西用久了,自然就会从仅使用的层面上升到探究其原理的层面,在javaweb中springmvc更是如此,越是优秀的框架,其底层实现代码更是复杂,而在我看来,一个优秀程序猿就相当于一名武林高手,不断 ...

  8. dict函数

    增 fromkeys(iterable, value) 用可迭代对象生成键,创建默认值相同的字典(value默认None) 删 pop(k) 通过k来删除字典元素, 找不到就会报错, 返回被删除字典元 ...

  9. 2018/11/5 每日分析-test

    郑醇1901,M30向上一笔中,只是看起来不太值得做,主要因为现在30分钟向上一笔空间无法判定,未必能上去(M5中枢如果向上突破并且不背驰才可能有机会:如果直接下去或者向上后背驰,那么这里就只是一个M ...

  10. 【Unix网络编程】chapter7套接字选项

    7.1 概述 有很多方法来获取和设置影响套接字的选项: getsockopt和setsockopt函数 fcntl函数 ioctl函数 7.2 getsockopt和setsockopt函数 7.3 ...