angular 实现总价满100折扣
2014-04-27 00:36
337 查看
<div ng-controller="CartController">
<div ng-repeat="item in items">
<span>{{item.title}}</span>
<input ng-model="item.quantity" />
<span>{{item.price|currency}}</span>
<span>{{item.price*item.quantity|currency}}</span>
</div>
<div>total:{{totalCart()|currency}}</div>
<div>Discount:{{bill.discount|currency}}</div>
<div>Subtotal:{{subtotal()|currency}}</div>
</div>
var app=angular.module("myApp",[]);
app.controller("CartController",function($scope){
$scope.bill={};
$scope.items=[{title:"paint pots",quantity:8,price:3.98},
{title:"polka dots",quantity:17,price:13.65},
{title:"pebbles",quantity:5,price:6.85},
{title:"ppts",quantity:18,price:33.09},
{title:"papses",quantity:98,price:45.68},
];
$scope.totalCart=function(){
var total=0;
for(var i=0,len=$scope.items.length;i<len;i++){
total=total+$scope.items[i].price*$scope.items[i].quantity;
}
return total;
}
$scope.subtotal=function(){
return $scope.totalCart()-$scope.bill.discount;
}
function calculateDiscount(newValue){
return $scope.bill.discount=newValue>1000?100:0;
}
$scope.$watch($scope.totalCart,calculateDiscount);
});
<div ng-repeat="item in items">
<span>{{item.title}}</span>
<input ng-model="item.quantity" />
<span>{{item.price|currency}}</span>
<span>{{item.price*item.quantity|currency}}</span>
</div>
<div>total:{{totalCart()|currency}}</div>
<div>Discount:{{bill.discount|currency}}</div>
<div>Subtotal:{{subtotal()|currency}}</div>
</div>
var app=angular.module("myApp",[]);
app.controller("CartController",function($scope){
$scope.bill={};
$scope.items=[{title:"paint pots",quantity:8,price:3.98},
{title:"polka dots",quantity:17,price:13.65},
{title:"pebbles",quantity:5,price:6.85},
{title:"ppts",quantity:18,price:33.09},
{title:"papses",quantity:98,price:45.68},
];
$scope.totalCart=function(){
var total=0;
for(var i=0,len=$scope.items.length;i<len;i++){
total=total+$scope.items[i].price*$scope.items[i].quantity;
}
return total;
}
$scope.subtotal=function(){
return $scope.totalCart()-$scope.bill.discount;
}
function calculateDiscount(newValue){
return $scope.bill.discount=newValue>1000?100:0;
}
$scope.$watch($scope.totalCart,calculateDiscount);
});
相关文章推荐
- Angularjs的那些事 – 视图的生命周期
- Triangular Sums
- 原创:形象的讲解angular中的$q与promise
- Angular.js 相关记录
- Django+angularJs
- 原创:Angular新手容易碰到的坑,随时更新,欢迎订阅
- AngularJS开发指南
- angularjs的事件 $broadcast and $emit and $on
- 看AngularJS
- AngularJS的学习--$on、$emit和$broadcast的使用
- AngularJS $route重新认识
- AngularJS and Asp.net MVC
- AngularJS 初印象------对比 Asp.net MVC
- angularJs form
- angularjs点滴
- AngularJs-ui modal 封装 dialog
- AngularJs-ui modal 传参数
- 说说AngularJs——自定义指令(三)
- 说说AngularJs——自定义指令(二)
- angularJs(一)指令