最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add items to a shopping cart in AngularJS - Stack Overflow

programmeradmin2浏览0评论

Angular beginner here. I'm trying to create a shopping cart for college. I need to add the name and price in a text input and after clicking a button the item is supposed to be added to a list. The thing is that whenever I press the button nothing happens, and I'm lost since the console doesn't tell me anything about what could be wrong. So, here's my code:

<!DOCTYPE html>
<html ng-app = "myApp">
<head>
    <title>Shopping Cart</title>
    <script src = ".3.14/angular.min.js">     </script>
    <script src = "app.js"></script>        
</head>
<body ng-controller = "myShoppingCart">

            <h1>Add to cart</h1>
            <form >
                    <p>Product: <input type = "text" ng-model = "nameProduct"></p>
                    <p>Price: <input type = "number" min = "0" step = "any" ng-model = "priceProduct"></p>
                    <input type = "submit" value = "Add" ng-click = "addProduct()">
            </form>

        </div>

        <div">
            <ul>
                <li ng-repeat = "product in products">
                    <span>{{product.name}}</span>
                    <span>{{product.price}}</span>
                    <span><input type = "number" min = "0" placeholder = "0" value = "0" ng-model = "amount"></span>
                    <span>{{product.price*amount}}</span>
                </li>
            </ul>
        </div>
  </body>
</html>

And this is my js code:

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

myApp.controller('myShoppingCart', function($scope) {

 $scope.products = [];

 function addProduct() {

    $scope.productos.push({nombre:$scope.nameProduct, price:$scope.priceProduct});
    $scope.nameProduct = "";
    $scope.priceProduct = "";
 }

});

Angular beginner here. I'm trying to create a shopping cart for college. I need to add the name and price in a text input and after clicking a button the item is supposed to be added to a list. The thing is that whenever I press the button nothing happens, and I'm lost since the console doesn't tell me anything about what could be wrong. So, here's my code:

<!DOCTYPE html>
<html ng-app = "myApp">
<head>
    <title>Shopping Cart</title>
    <script src = "https://ajax.googleapis./ajax/libs/angularjs/1.3.14/angular.min.js">     </script>
    <script src = "app.js"></script>        
</head>
<body ng-controller = "myShoppingCart">

            <h1>Add to cart</h1>
            <form >
                    <p>Product: <input type = "text" ng-model = "nameProduct"></p>
                    <p>Price: <input type = "number" min = "0" step = "any" ng-model = "priceProduct"></p>
                    <input type = "submit" value = "Add" ng-click = "addProduct()">
            </form>

        </div>

        <div">
            <ul>
                <li ng-repeat = "product in products">
                    <span>{{product.name}}</span>
                    <span>{{product.price}}</span>
                    <span><input type = "number" min = "0" placeholder = "0" value = "0" ng-model = "amount"></span>
                    <span>{{product.price*amount}}</span>
                </li>
            </ul>
        </div>
  </body>
</html>

And this is my js code:

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

myApp.controller('myShoppingCart', function($scope) {

 $scope.products = [];

 function addProduct() {

    $scope.productos.push({nombre:$scope.nameProduct, price:$scope.priceProduct});
    $scope.nameProduct = "";
    $scope.priceProduct = "";
 }

});
Share Improve this question asked Mar 9, 2015 at 10:30 Gabo EremitaGabo Eremita 131 gold badge1 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You have pushed the values to wrong object . and also you need to change lot .and your button click should be need write to

 $scope.addProduct= function () {
//code
}

So please copy and past my code to instead of your code

HTML

<!DOCTYPE html>
<html ng-app = "myApp">
<head>
    <title>Shopping Cart</title>
    <script src = "https://ajax.googleapis./ajax/libs/angularjs/1.3.14/angular.min.js">     </script>
    <script src = "app.js"></script>        
</head>
<body ng-controller = "myShoppingCart">

            <h1>Add to cart</h1>
            <form >
                    <p>Product: <input type = "text" ng-model = "nameProduct"></p>
                    <p>Price: <input type = "number" min = "0" step = "any" ng-model = "priceProduct"></p>
                    <input type = "submit" value = "Add" ng-click = "addProduct()">
            </form>

        </div>

        <div>
            <ul>
                <li ng-repeat = "product in products">
                    <span>{{product.name}}</span>
                    <span>{{product.price}}</span>
                    <span><input type = "number" min = "0" placeholder = "0" value = "0" ng-model = "amount"></span>
                    <span>{{product.price*amount}}</span>
                </li>
            </ul>
        </div>
  </body>
</html>

and JS Code

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

myApp.controller('myShoppingCart', function($scope) {

 $scope.products = [];

$scope.addProduct= function () {

    $scope.products.push({name:$scope.name, price:$scope.priceProduct});
    $scope.nameProduct = "";
    $scope.priceProduct = "";
 }

});
<input type = "submit" value = "Add" ng-click = "addProduct()">

Needs to be

<input type = "button" value = "Add" ng-click = "addProduct()">

Submit will submit the form to the server, not exactly what you're looking for I guess.

Also, bad typo here:

 <div">

And here (products, not productos):

$scope.productos
发布评论

评论列表(0)

  1. 暂无评论