首頁 > web前端 > js教程 > 詳解angular.js實現購物車功能

詳解angular.js實現購物車功能

小云云
發布: 2018-01-04 09:55:52
原創
1974 人瀏覽過

本文主要為大家詳細介紹了angular.js購物車功能的實作程式碼,具有一定的參考價值,有興趣的朋友們可以參考一下,希望能幫助到大家。

本文範例為大家分享了angular.js購物車功能的具體程式碼,供大家參考,具體內容如下


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <title>购物车</title>

  <script src="angularjs/angular.js"></script>

  <style>

    .box{

      width: 100%;

      border-bottom: 1px solid silver;

    }

    .box1{

      width: 100%;

      margin-top: 5px;

    }

    .box1 button{

      width: 100px;

      height: 40px;

      background: crimson;

      color: white;

      text-align: center;

      line-height: 40px;

      float: right;

      border: 0;

      border-radius: 13px;

    }

    table{

      width: 100%;

    }

    tr td button{

      background: blue;

      color: white;

      border: 0;

    }

 

  </style>

  <script>

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

    my.controller("mys",function ($scope) {

      /*声明数据对象,初始化商品信息,数据自拟且不低于四条*/

      $scope.arr=[

        {name:"qq",price:12.9,number:2,state:false},

        {name:"wx",price:23.9,number:1,state:false},

        {name:"aa",price:99.9,number:1,state:false},

        {name:"bb",price:10.9,number:5,state:false}

      ];

      /*删除条目*/

      $scope.del=function (index) {

        if(confirm("确定移除此项嘛?")){

          $scope.arr.splice(index,1);

        }

      }

      /*点击”+”按钮输入框中的数量加1,同时可以计算出商品小计,和购物 车总价*/

      $scope.jia=function (index) {

        $scope.arr[index].number++;

      }

      /*当点击”-”按钮时输入框中的数量减1,商品小计和总价*/

      $scope.jian=function (index) {

        if($scope.arr[index].number>1){

          $scope.arr[index].number--;

        }

        else if($scope.arr[index].number==1){

          if(confirm("用户是否删除该商品")){

            $scope.arr.splice(index,1);

          }

        }

      }

      /*计算总价格*/

      $scope.allSum=function () {

        var allPrice=0;

        for(var i=0;i<$scope.arr.length;i++){

          allPrice+=$scope.arr[i].price*$scope.arr[i].number;

        }

        return allPrice;

      };

      /*清空购物车*/

      $scope.alldel=function () {

        if($scope.arr.length==0){

          alert("您的购物车已空");

        }else{

          $scope.arr=[];

        }

      }

      /*用户点击第一个checkbox代表全选,全选商品后点击删除选中商品,选中商品被删除,  若购物车商品被全部删除后*/

      $scope.allCheck=false;

      $scope.allx= function () {

        for(var i=0;i<$scope.arr.length;i++){

          if($scope.allCheck==true){

            $scope.arr[i].state=true;

          }else {

            $scope.arr[i].state=false;

          }

        }

      };

      /*每个复选框*/

      $scope.itemCheck = function () {

        var flag = 0;

        for(var i = 0; i<$scope.arr.length; i++){

          if($scope.arr[i].state == true){

            flag ++;

          }

        }

        if(flag == $scope.arr.length){

          $scope.allCheck = true;

        }else{

          $scope.allCheck = false;

        }

      };

      /*批量删除*/

      $scope.pi=function () {

        for(var i=0;i<$scope.arr.length;i++){

          if($scope.arr[i].state==true){

            $scope.arr.splice(i,1);

            i--;

            $scope.allCheck = false;

          }

        }

      }

    });

  </script>

</head>

<body ng-app="my" ng-controller="mys">

  <p class="box">

    <h2>我的购物车</h2>

  </p>

  <p class="box1">

    <button ng-click="alldel()" style="margin-right: 10px">清空购物车</button><button ng-click="pi()" style="margin-left: 5px">批量删除</button>

  </p>

  <p class="box2">

    <table border="1">

      <tr>

        <th><input type="checkbox" ng-model="allCheck" ng-click="allx()"/></th>

        <th>name</th>

        <th>price</th>

        <th>number</th>

        <th>totalPrice</th>

        <th>option</th>

      </tr>

      <!--用ng-repaet指令将对象遍历并渲染到页面中-->

      <tr ng-repeat="item in arr">

        <td><input type="checkbox" ng-model="item.state" ng-click="itemCheck()"/></td>

        <td>{{item.name}}</td>

        <td>{{item.price | currency:"¥:"}}</td>

        <td><button ng-click="jian($index)">-</button>

          <input type="text" value="{{item.number}}" style="width: 30px;padding-left: 20px"/>

          <button ng-click="jia($index)">+</button>

        </td>

        <td>{{item.price*item.number | currency:"¥:"}}</td>

        <td><button ng-click="del($index)">删除</button></td>

      </tr>

      <tr>

        <td colspan="6">总金额<span ng-bind="allSum()|currency:&#39;¥:&#39;"></span></td>

      </tr>

    </table>

  </p>

</body>

</html>

登入後複製

相關推薦:

php簡單實作加入購物車功能案例

javascript實作簡易版購物車功能

##php實作購物車功能實例講解

以上是詳解angular.js實現購物車功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板