AngularJS分层(代码分离)

1.页面引入js

        

2.各个js

base.js

var app = angular.module("pinyougou",[]);

base_pagenation.js

var app = angular.module("pinyougou",["pagination"]);

baseController.js

app.controller("baseController",function($scope){
	//分页控件配置 ,插件加载的时候onChange方法会自动执行一次
	$scope.paginationConf = {
		 currentPage: 1,//当前页
		 totalItems: 10,//总记录数
		 itemsPerPage: 10,//每页记录数
		 perPageOptions: [10, 20, 30, 40, 50],//分页选项
		 onChange: function(){//页码变更函数
			 $scope.reloadList();
		 }
	}; 
	
	//刷新列表
	$scope.reloadList = function(){
		 $scope.search($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage);
	}
	
	$scope.selectIds = [];//用户勾选的id集合
	//更新选中的ids
	$scope.updateSelection = function($event,id){
		if($event.target.checked){
			$scope.selectIds.push(id);
		}else{
			var index = $scope.selectIds.indexOf(id);//查找值的位置
			$scope.selectIds.splice(index, 1);//参数:移除的位置,移除的个数
		}
	}
	//提取json字符串数据中某个属性,返回拼接字符串,逗号分隔
	$scope.jsonToString = function(jsonString,key){
		var json = JSON.parse(jsonString);
		var value="";
		for (var i = 0; i < json.length; i++) {
			if(i>0){
				value += ",";
			}
			value += json[i][key];//var a = {"id":"1","text":"联想"};  取属性2中方式: a.id a['id']
		}
		return value;
	}
})

brandController.js

app.controller("brandController",function($scope,$http,$controller,brandService){
		
	$controller("baseController",{$scope:$scope});
	
		//查询品牌列表数据
		$scope.findAll = function(){
			brandService.findAll().success(
					function(response){
						$scope.list = response;
					}
			)
		}
		
		
		//分页
		$scope.findPage = function(page,size){
			brandService.findPage(page,size).success(function(response){
				$scope.list = response.rows;//显示当前页数据
				$scope.paginationConf.totalItems = response.total;//更改总记录数
			});
		}
		
		//新增
		$scope.save = function(){
			var object = null;//方法名
			if($scope.entity.id != null){
				object = brandService.update($scope.entity);
			}else{
				object = brandService.add($scope.entity);
			}
			object.success(function(res){
				if(res.success){
					$scope.reloadList();
				}else{
					alert(res.message);
				}
			})
		}
		
		//查询实体
		$scope.findOne = function(id){
			brandService.findOne(id).success(function(res){
				$scope.entity = res;//显示当前页数据
			});
		}
		
		
		//删除
		$scope.dele = function(){
			brandService.dele($scope.selectIds).success(function(res){
				if(res.success){
					$scope.reloadList();
					$scope.selectIds = [];//重置
				}else{
					alert(res.message);
				}
			});
		}
		
		$scope.searchEntity = {};
		//条件查询
		$scope.search = function(page,size){
			//注意此处的传参
			brandService.search(page,size,$scope.searchEntity).success(function(res){
				$scope.list = res.rows;//显示当前页数据
				$scope.paginationConf.totalItems = res.total;//更改总记录数
			});
		}
		
		
	});

brandService.js

//品牌服务
app.service("brandService",function($http){
	this.findAll = function(){
		return $http.get("/brand/findAll.do");
	}
	this.findPage = function(page,size){
		return $http.get("/brand/findPage.do?page="+page+"&size="+size);
	}
	this.findOne = function(id){
		return $http.get("/brand/findOne.do?id="+id);
	}
	this.dele = function(ids){
		return $http.get("/brand/delete.do?ids="+ids);
	}
	this.search = function(page,size,searchEntity){
		return $http.post("/brand/search.do?page="+page+"&size="+size, searchEntity);
	}
	this.add = function(entity){
		return $http.post("/brand/add.do",entity);
	}
	this.update = function(entity){
		return $http.post("/brand/update.do",entity);
	}
	//下拉列表数据
	this.selectOptionList = function(){
		return $http.get("/brand/selectOptionList.do");
	}
})

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值