简体   繁体   English

将项目添加到AngularJS的购物车中

[英]Add items to a shopping cart in AngularJS

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.com/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: 这是我的js代码:

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 = "";
 }

});
<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

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 的HTML

<!DOCTYPE html>
<html ng-app = "myApp">
<head>
    <title>Shopping Cart</title>
    <script src = "https://ajax.googleapis.com/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 和JS代码

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 = "";
 }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM