var controller1 = function($scope) {
$scope.cart = [{
id: 1243,
name: '西游记',
price: 32.5,
number: 2
},
{
id: 2345,
name: '红楼梦',
price: 32,
number: 4
},
{
id: 4323,
name: '水浒传',
price: 34,
number: 2
},
{
id: 6345,
name: '三国演义',
price: 56,
number: 1
}
];
/**
* 为数量进行减一操作
*/
$scope.reduce = function(id) {
var index = findIndex(id);
if(index != -1) {
--$scope.cart[index].number;
}
}
/**
* 删除一个数据
*/
$scope.remove=function(index){
$scope.cart.splice(index,1);
}
/**
* 减一不得小与1
*/
$scope.$watch('cart',function(newValue,oldValue){
angular.forEach($scope.cart,function(obj,key){
if(obj.number<1){
if(confirm("您确定要删除吗?")){
$scope.cart.splice(key,1);
}else{
$scope.cart[key].number=1;
}
}
});
},true);
/**
* 为数量进行加一操作
*/
$scope.add = function(id) {
var index = findIndex(id);
if(index != -1) {
++$scope.cart[index].number;
}
}
/**
* 计算总价格
*/
$scope.sumPrice = function(){
var sumMoney = 0;
angular.forEach($scope.cart,function(item,key){
sumMoney+=item.number*item.price;
});
return sumMoney;
}
/**
* 按照id查找到一个下标
*/
var findIndex = function(id) {
var index = -1;
angular.forEach($scope.cart, function(item, key) {
if(item.id == id) {
index = key;
return;
}
});
return index;
}
}
$scope.cart = [{
id: 1243,
name: '西游记',
price: 32.5,
number: 2
},
{
id: 2345,
name: '红楼梦',
price: 32,
number: 4
},
{
id: 4323,
name: '水浒传',
price: 34,
number: 2
},
{
id: 6345,
name: '三国演义',
price: 56,
number: 1
}
];
/**
* 为数量进行减一操作
*/
$scope.reduce = function(id) {
var index = findIndex(id);
if(index != -1) {
--$scope.cart[index].number;
}
}
/**
* 删除一个数据
*/
$scope.remove=function(index){
$scope.cart.splice(index,1);
}
/**
* 减一不得小与1
*/
$scope.$watch('cart',function(newValue,oldValue){
angular.forEach($scope.cart,function(obj,key){
if(obj.number<1){
if(confirm("您确定要删除吗?")){
$scope.cart.splice(key,1);
}else{
$scope.cart[key].number=1;
}
}
});
},true);
/**
* 为数量进行加一操作
*/
$scope.add = function(id) {
var index = findIndex(id);
if(index != -1) {
++$scope.cart[index].number;
}
}
/**
* 计算总价格
*/
$scope.sumPrice = function(){
var sumMoney = 0;
angular.forEach($scope.cart,function(item,key){
sumMoney+=item.number*item.price;
});
return sumMoney;
}
/**
* 按照id查找到一个下标
*/
var findIndex = function(id) {
var index = -1;
angular.forEach($scope.cart, function(item, key) {
if(item.id == id) {
index = key;
return;
}
});
return index;
}
}