How to implement a simple shopping cart in angularJS
Mar 31, 2018 pm 05:07 PMThis article mainly shares with you how to implement a simple shopping cart in angularJS. I hope it can help you.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>我的購(gòu)物車</title> <script type="text/javascript" src="../js/angular.min.js" ></script> <script type="text/javascript" src="../js/jquery-3.3.1.min.js" ></script> <style> * { margin: 0; padding: 0; } table { border-collapse: collapse; } td, th { padding: 3px; height: 50px; width: 100px; border: 1px solid gainsboro; text-align: left; } .nav { width: 800px; text-align: right; } tbody tr td:nth-child(4) { width: 200px; } tbody tr td:nth-child(4) button { width: 30px; height: 30px; } tbody tr td:nth-child(4) input { width: 30px; height: 30px; border: 1px solid gainsboro; border-radius: 5px; } .nav button { width: 150px; height: 40px; background: red; color: white; border: 0; border-radius: 5px; } button { width: 50px; height: 30px; background: blue; border: 0; border-radius: 5px; color: white; } a { text-decoration: none; } </style> <script> var myapp = angular.module("myapp", []); myapp.controller("myCtrl", function ($scope) { //自擬商品信息 $scope.data = [{"name": "qq", "price": 12.9, "number": 1, done: false}, {"name": "wx", "price": 23.9, "number": 1, done: false}, {"name": "wx", "price": 99.9, "number": 1, done: false}, {"name": "wb", "price": 63.9, "number": 1, done: false}]; //點(diǎn)擊加號(hào)的方法 $scope.add = function (index) { $scope.data[index].number++; $scope.sum(); } //點(diǎn)擊減號(hào)的方法 $scope.jian = function (index) { //點(diǎn)擊-操作時(shí),當(dāng)商品數(shù)量為1時(shí),彈出對(duì)話框,詢問(wèn)是否移除 if ($scope.data[index].number == 1) { if (confirm("您是否將該商品移除購(gòu)物車?")) { $scope.data[index].number--; $scope.data.splice(index,1); $scope.sum(); } } else if ($scope.data[index].number > 1) { $scope.data[index].number--; $scope.sum(); } } //計(jì)算商品總價(jià)的方法 $scope.sum = function () { $scope.allMoney = 0; for (var i = 0; i < $scope.data.length; i++) { $scope.allMoney += $scope.data[i].price * $scope.data[i].number; } } $scope.sum(); //點(diǎn)擊輸入框的方法 $scope.dianji = function () { $scope.sum(); } $scope.shopping = false; //默認(rèn)全選是不選的 $scope.check = false; //刪除全部商品的方法 $scope.delAll = function (check) { $scope.checkD(check); } $scope.checkD = function (state) { for (var i = 0; i < $scope.data.length; i++) { $scope.data[i].done = state; } } //下面的選框代表 $scope.checkSS = false; $scope.checkS = function () { $scope.flag = 0; for (var i = 0; i < $scope.data.length; i++) { if ($scope.data[i].done == true) { $scope.flag++; $scope.checkSS = true; } } //實(shí)現(xiàn)當(dāng)下面全部選中,全選框選中的效果 if ($scope.flag == $scope.data.length) { $scope.check = true; } else { $scope.check = false; } } //判斷全選框下面的多選框有幾個(gè)的方法 //清空購(gòu)物車的方法 $scope.clearShpooing = function () { if ($scope.check == true || $scope.checkSS == true) { for (var i = 0; i < $scope.data.length; i++) { if ($scope.data[i].done == true) { $scope.data.splice(i--, 1); if ($scope.data.length == 0) { $scope.show = false; $scope.shopping = true; } $scope.sum(); } } if ($scope.check == true) { $scope.data.length = 0; $scope.show = false; $scope.shopping = true; } } else { alert("請(qǐng)先選擇要操作的商品"); } } $scope.show = true; $scope.remove = function (index) { $scope.data.splice(index, 1); if ($scope.data.length == 0) { $scope.show = false; $scope.shopping = true; } } //點(diǎn)擊去逛商城的代碼 $scope.showS=function () { $scope.show = true; $scope.check=false; } }); </script> </head> <body ng-app="myapp" ng-controller="myCtrl"> <h1>我的購(gòu)物車</h1> <p ng-show="shopping">您的購(gòu)物車為空<a href="#" ng-click="showS()">去逛商城</a></p> <p class="nav"> <button ng-click="clearShpooing()" ng-show="show">清空購(gòu)物車</button> </p> <!--創(chuàng)建購(gòu)物車列表--> <table ng-show="show"> <thead> <th><input type="checkbox" ng-model="check" ng-click="delAll(check)"></th> <th>name</th> <th>price</th> <th>number</th> <th>totalprice</th> <th>option</th> </thead> <tbody> <!--ng-repeat渲染數(shù)據(jù)到界面--> <tr ng-repeat="item in data"> <td><input type="checkbox" ng-model="item.done" ng-click="checkS()"></td> <td>{{item.name}}</td> <td>{{item.price | currency:"¥"}}</td> <td> <button ng-click="jian($index)">-</button> <input type="text" ng-model="item.number" ng-checked="dianji()"> <button ng-click="add($index)">+</button> </td> <td>{{item.price*item.number |currency:"¥"}}</td> <td> <button ng-click="remove($index)">刪除</button> </td> </tbody> <tfoot> <tr> <!--價(jià)錢起前面加"¥"--> <td colspan="6">總價(jià)為:{{allMoney | currency:"¥"}}</td> </tr> </tfoot> </table> </body> </html>
Related recommendations:
Write a simple shopping cart function in JavaScript
The above is the detailed content of How to implement a simple shopping cart in angularJS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Practical tutorial: Detailed explanation of the shopping cart function with PHP and MySQL. The shopping cart function is one of the common functions in website development. Through the shopping cart, users can easily add the goods they want to buy to the shopping cart, and then proceed with settlement and payment. In this article, we will detail how to implement a simple shopping cart function using PHP and MySQL and provide specific code examples. To create a database and data table, you first need to create a data table in the MySQL database to store product information. The following is a simple data table

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
