service层,service层专门用来向后台发送请求
//定义Service层
app.service('brandService',function($http){
this.selectOptionList=function () {
return $http.get("../brand/selectOptionList.do")
}
//获取分页数据
this.findPage=function (page,rows) {
return $http.get('../brand/pageAll.do?pageNum='+page+'&pageSize='+rows);
}
//添加
this.save=function (entity) {
return $http.post("../brand/save.do",entity);
}
//修改
this.update=function (entity) {
return $http.post("../brand/update.do",entity);
}
//删除
this.delete=function (selectIds) {
return $http.get("../brand/delete.do?ids="+selectIds);
}
//查询单个
this.findOne=function (id) {
return $http.get('../brand/findOne.do?id='+id);
}
//添加查询家分页
this.search=function (pageNum,pageSize,searchEntity) {
return $http.post('../brand/search.do?pageNum='+pageNum+'&pageSize='+pageSize,searchEntity);
}
})
定义父类controller专门用来存放公用的方法 比如分页 批量选中
//基本控制层
app.controller('baseController',function ($scope) {
$scope.reloadList=function(){
//切换页码
//$scope.findPage( $scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);
//切换页码时吧全选那个选中清空
$scope.all="";
$scope.search( $scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);
}
//分页控件配置
$scope.paginationConf = {
currentPage: 1,
totalItems: 0,
itemsPerPage:7,
perPageOptions: [10, 20, 30, 40, 50],
onChange: function(){
$scope.reloadList();//重新加载
}
};
//获取被选中的id
$scope.selectIds = [];
//获取全部被选中的id
$scope.chick=function(){
$scope.selectIds = [];
angular.forEach($scope.list,function (entity) {
if (!entity.checked){
entity.checked=true;
$scope.selectIds.push(entity.id);
}else{
entity.checked=false;
}
})
}
//更新复选
$scope.updateSelection = function($event,entity){
if($event.target.checked){//如果是被选中,则增加到数组
$scope.selectIds.push(entity.id);
}else{
var idx = $scope.selectIds.indexOf(entity.id);
$scope.selectIds.splice(idx, 1);//删除
}
}
//提取json字符串数据中某个属性,返回拼接字符串 逗号分隔
$scope.jsonToString=function(jsonString,key){
var json=JSON.parse(jsonString);//将json字符串转换为json对象
var value="";
for(var i=0;i<json.length;i++){
if(i>0){
value+=","
}
value+=json[i][key];
}
return value;
}
})
继承
app.controller('brandController' ,function($scope,$controller,brandService){
$controller('baseController',{$scope:$scope});//继承
controller类专门写事件方法 继承父类通用方法
//定义控制层,需要注入service代码
app.controller('brandController' ,function($scope,$controller,brandService){
$controller('baseController',{$scope:$scope});//继承
//添加 家后缀名.do 要不然报404
$scope.save=function(){
var serviceObject;
if ($scope.entity.id!=null){
//修改
serviceObject=brandService.update($scope.entity);
}else {
//添加
serviceObject= brandService.save($scope.entity);
}
//共同的方法 修改时吧修改的数据
serviceObject.success(
function (response) {
if(response.success){
//重新查询
$scope.reloadList();;//重新加载
$scope.entity = "";
}else{
alert(response.message);
}
})
}
//查看实体类信息
//回显单个数据
$scope.findOne=function(id){
brandService.findOne(id).success(
function(response){
$scope.entity=response; //显示当前页的数据
})
}
//批量删除
$scope.delete=function(){
if (confirm("您确定要删除吗?")){
brandService.delete($scope.selectIds).success(
function (response) {
if (response.success){
$scope.reloadList();
$scope.selectIds = [];
}else{
alert(response.message)
}
})
}
}
//默认为空 搜索框不写数据
$scope.searchEntity={};
//条件查询家分页
$scope.search=function(pageNum,pageSize){
brandService.search(pageNum,pageSize,$scope.searchEntity).success(
function (response) {
$scope.list=response.rows; //显示当前页的数据
$scope.paginationConf.totalItems=response.total;//更新总记录数
})
}
})
在HTML中引入,按照顺序摆放
<!--加载angular.js-->
<script src="../plugins/angularjs/angular.min.js"></script>
<!--加载分页-->
<script src="../plugins/angularjs/pagination.js"></script>
<link rel="stylesheet" href="../plugins/angularjs/pagination.css">
<!--调取带分页的组件-->
<script src="../js/base_pagination.js"></script>
<script src="../js/service/brandService.js"></script>
<script src="../js/controller/baseController.js"></script>
<script src="../js/controller/brandController.js"></script>