AngularJS implement a simple shopping cart

Source: Internet
Author: User

Using ANGULARJS to implement a simple shopping cart, the main feeling is that powerful two-way bindings and focus-only objects do not pay attention to the interface features.

First look at the interface:

Click + + to operate and delete:

These all only need to manipulate the data source on the line, do not need to focus on the interface.

Implementation process:

One, use any language to create a server side:
     Public class Shoppingcar    {        publicstringgetset;}          Public decimal Get Set ; }          Public int Get Set ; }    }
         PublicActionResult Getcar () {List<ShoppingCar> cars =NewList<shoppingcar>            {                NewShoppingcar {title="Apple", count=1, unitprice=2.5m},                NewShoppingcar {title="Banana", count=3, unitprice=1.5m},                NewShoppingcar {title="Balsam Pear", count=1, unitprice=3.5m},                NewShoppingcar {title="Cucumber", count=3, unitprice=2.2m}            }; returnJson (Cars,jsonrequestbehavior.allowget); }         PublicActionResult Addcar (list<shoppingcar>car) {            returnJson ("OK", Jsonrequestbehavior.allowget); }
Second, the front office implementation:
    <DivNg-app= "DEMOAPP"Ng-controller= ' Cartcontroller '>        <Tableclass= "Table table-striped">            <thead>                <TR>                    <TD>Title</TD>                    <TD>Price</TD>                    <TD>Number</TD>                    <TD>Subtotal</TD>                    <TD>Delete</TD>                </TR>            </thead>            <tbody>                <TRng-repeat= "Item in Shoppingcar">                    <TD>{Item.} Title}}</TD>                    <TD>{Item.} UnitPrice}}</TD>                    <TD>                        <inputtype= "text"Ng-cloak Ng-model= "Item. Count "style= "Width:50px;text-align:center;">                        <ButtonMy-adds Ng-click= "Updatecar" (item. title,1) "Ng-class= "{cursors:true}">+</Button>                        <ButtonMy-minus Ng-click= "Updatecar" (item. title,-1) "Ng-class= "{cursors:true}">-</Button>                    </TD>                    <TD>{Item.} Count*item. UnitPrice | Number:2}}</TD>                    <TD><ButtonMy-minus Ng-click= "Updatecar" (item. title,-100) "Ng-class= "{cursors:true}">By deleting</Button></TD>                </TR>            </tbody>        </Table>        <PNg-init=0>Overall Price: {{total | number:2}}</P>        <Buttontype= "button"Ng-click= "Submit ()">Submit</Button>    </Div>
Third, angular part
    varApp = Angular.module (' DemoApp ', []); App.controller (' Cartcontroller ', [' $scope ', ' $http ',function($scope, $http) {$scope. Shoppingcar= {}        varGetcar =function() {$http. Get ('/employee/getcar '). Success (function(response) {$scope. Shoppingcar=response;            Gettotal ();        }); } $scope. Total= 0; varGettotal =function () {             for(vari = 0; I < $scope. Shoppingcar.length; i++) {                varitem =$scope.                Shoppingcar[i]; $scope. Total+ = Item. Count *item.            UnitPrice; }} $scope. Updatecar=function(title, count) { for(vari = 0; I < $scope. Shoppingcar.length; i++) {                varitem =$scope.                Shoppingcar[i]; if(item. Title = =title) {Item. Count= Item. Count + count;//Here you can increase the upper and lower limits                    if(item. Count < 0) {$scope. Shoppingcar.splice (i,1);        }}} gettotal (); } $scope. Submit=function() {$http. Post ('/employee/addcar ', $scope. Shoppingcar). Success (function(response) {alert (response);        });    } getcar (); }]);

AngularJS implement a simple shopping cart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.