亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Angular.js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能

 更新時(shí)間:2016年11月21日 17:12:41   作者:騎著小豬來(lái)編程  
在各大購(gòu)物網(wǎng)站大家都可以簡(jiǎn)單購(gòu)物車(chē)效果演示,下面通過(guò)本文給大家分享一段代碼關(guān)于使用Angular.js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能,需要的朋友可以參考下

先給大家分享實(shí)現(xiàn)代碼,在代碼下面有效果圖展示,大家可以?xún)烧呓Y(jié)合參考下,廢話不多說(shuō)了,具體代碼如下所示:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://code.angularjs.org/1.2.5/angular.min.js"></script>
<style type="text/css">
td,th{
width: 150px;
text-align: left;
}
table{
width: 800px;
}
.num{
width: 70px;
text-align: center;
}
tr td:last-child button {
background-color: red;
}
#foot button{
background-color: red;
}
</style>
</head>
<!--ng-bind是從$scope -> view的單向綁定ng-modle是$scope <-> view的雙向綁定
{{}} 與 ng-bind 的區(qū)別是后者在加載時(shí)用戶(hù)不會(huì)看到渲染之前的東西,前者可能會(huì)看到,所以首頁(yè)一般用后者加載數(shù)據(jù)
-->
<body ng-app="myApp">
<div ng-controller="VC1">
<table border="" cellspacing="" cellpadding="">
<tr><th>產(chǎn)品編號(hào)</th><th>產(chǎn)品名稱(chēng)</th><th>購(gòu)買(mǎi)數(shù)量</th><th>產(chǎn)品單價(jià)</th><th>產(chǎn)品總價(jià)</th><th>操作</th></tr>
<tr ng-repeat="x in Product" >
<td>{{x.id}}</td>
<td>{{x.name}}</td>
<td>
<button ng-click="reduce($index)">-</button>
<input type="text" class="num" ng-model="x.quantity" ng-change="change($index)" />
<button ng-click="add($index)">+</button> </td>
<td >{{x.price}}</td>
<td>{{x.price * x.quantity}}</td>
<td><button ng-click="remove($index)">移除</button></td></tr>
</table>
<div id="foot"><span>總價(jià):</span><span ng-bind="totalQuantity()"></span><span>購(gòu)買(mǎi)數(shù)量</span>
<span >{{numAll()}}</span> <button ng-click="removeAll()">清空購(gòu)物車(chē)</button> </div>
</div>
</body>
<script type="text/javascript">
var app = angular.module("myApp",[]);
app.controller("VC1",function($scope){
$scope.Product = [{
id: 1000,
name: "iPhone8",
quantity: 1,
price: 8888
}, {
id: 1001,
name: "iPhone9",
quantity: 1,
price: 9888
}, {
id: 1002,
name: "iPhone 2s",
quantity: 1,
price: 3888
}, {
id: 1003,
name: "iPhone 7P+",
quantity: 1,
price: 10088
}];
//減少數(shù)量
$scope.reduce = function(index){
if( $scope.Product[index].quantity > 1){
$scope.Product[index].quantity--;
}else{
$scope.remove(index);
}
}
//添加數(shù)量函數(shù)
$scope.add = function(index){
$scope.Product[index].quantity++;
}
//所有商品總價(jià)函數(shù)
$scope.totalQuantity = function(){
var allprice = 0
for(var i = 0 ; i <$scope.Product.length;i++ ){
allprice += $scope.Product[i].quantity * $scope.Product[i].price;
}
return allprice;
}
//購(gòu)買(mǎi)總數(shù)量函數(shù)
$scope.numAll = function(){
var numAlls = 0
for(var i = 0 ; i <$scope.Product.length;i++ ){
numAlls += $scope.Product[i].quantity;
}
return numAlls;
}
//刪除當(dāng)前商品
$scope.remove = function(index){
if(confirm("確定要清空數(shù)據(jù)嗎")){
$scope.Product.splice(index,1)
}
}
//清空購(gòu)物車(chē)
$scope.removeAll = function(){
if(confirm("你確定套清空購(gòu)物車(chē)所有商品嗎?")){
$scope.Product = [];
}
}
//解決輸入框輸入負(fù)數(shù)時(shí)變?yōu)?
$scope.change = function(index){
if ( $scope.Product[index].quantity >= 1) {
}else{
$scope.Product[index].quantity = 1;
}
}
$scope.$watch('Product',function(oldvalue,newvalue){
console.log(oldvalue);
console.log(newvalue);
})
})
</script>
</html>

效果圖展示如下:

以上所述是小編給大家介紹的使用Angular.js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Ionic + Angular.js實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能的方法

    Ionic + Angular.js實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能的方法

    驗(yàn)證碼倒計(jì)時(shí)這個(gè)功能相信對(duì)大家每個(gè)人來(lái)說(shuō)都不陌生,之前介紹了在Android中的實(shí)現(xiàn)方法,下面這篇文章主要給大家介紹了利用Ionic + Angular.js實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • 基于angularJS的表單驗(yàn)證指令介紹

    基于angularJS的表單驗(yàn)證指令介紹

    下面小編就為大家?guī)?lái)一篇基于angularJS的表單驗(yàn)證指令介紹。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • angularJS之$http:與服務(wù)器交互示例

    angularJS之$http:與服務(wù)器交互示例

    $http是angular中的一個(gè)核心服務(wù),本篇文章主要介紹了angularJS之$http:與服務(wù)器交互示例,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-03-03
  • Angular4集成ng2-file-upload的上傳組件

    Angular4集成ng2-file-upload的上傳組件

    本篇文章主要介紹了Angular4集成ng2-file-upload的上傳組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • AngularJS使用攔截器實(shí)現(xiàn)的loading功能完整實(shí)例

    AngularJS使用攔截器實(shí)現(xiàn)的loading功能完整實(shí)例

    這篇文章主要介紹了AngularJS使用攔截器實(shí)現(xiàn)的loading功能,結(jié)合完整實(shí)例形式分析了AngularJS攔截器的設(shè)置、調(diào)用及l(fā)oading功能實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-05-05
  • Angular實(shí)現(xiàn)的table表格排序功能完整示例

    Angular實(shí)現(xiàn)的table表格排序功能完整示例

    這篇文章主要介紹了Angular實(shí)現(xiàn)的table表格排序功能,結(jié)合完整實(shí)例形式分析了AngularJS表格排序所涉及的事件響應(yīng)、元素遍歷、屬性修改等相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • AngularJS實(shí)現(xiàn)DOM元素的顯示與隱藏功能

    AngularJS實(shí)現(xiàn)DOM元素的顯示與隱藏功能

    這篇文章主要介紹了AngularJS實(shí)現(xiàn)DOM元素的顯示與隱藏功能,涉及AngularJS中ng-hide與ng-show兩個(gè)屬性的使用,需要的朋友可以參考下
    2016-11-11
  • AngularJS 依賴(lài)注入詳解及示例代碼

    AngularJS 依賴(lài)注入詳解及示例代碼

    本文主要介紹AngularJS 依賴(lài)注入的知識(shí),這里整理了相關(guān)的基礎(chǔ)知識(shí),并附示例代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下
    2016-08-08
  • Angular+Ionic使用queryParams實(shí)現(xiàn)跳轉(zhuǎn)頁(yè)傳值的方法

    Angular+Ionic使用queryParams實(shí)現(xiàn)跳轉(zhuǎn)頁(yè)傳值的方法

    這篇文章主要介紹了Angular+Ionic使用queryParams實(shí)現(xiàn)跳轉(zhuǎn)頁(yè)傳值的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 詳解ng-alain動(dòng)態(tài)表單SF表單項(xiàng)設(shè)置必填和正則校驗(yàn)

    詳解ng-alain動(dòng)態(tài)表單SF表單項(xiàng)設(shè)置必填和正則校驗(yàn)

    這篇文章主要介紹了詳解ng-alain動(dòng)態(tài)表單SF表單項(xiàng)設(shè)置必填和正則校驗(yàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06

最新評(píng)論