javascript 在 AngularJS 中将商品添加到购物车
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28939784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add items to a shopping cart in AngularJS
提问by Gabo Eremita
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:
Angular 初学者在这里。我正在尝试为大学创建一个购物车。我需要在文本输入中添加名称和价格,在单击按钮后,该项目应该添加到列表中。问题是,每当我按下按钮时,什么都没有发生,而且我迷路了,因为控制台没有告诉我任何可能出错的地方。所以,这是我的代码:
<!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 = "";
}
});
回答by
<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.
Submit 会将表单提交到服务器,我猜这不完全是您要查找的内容。
Also, bad typo here:
另外,这里的错字错误:
<div">
And here (products, not productos):
在这里(产品,而不是产品):
$scope.productos
回答by Ramesh Rajendran
You have pushed the values to wrong object . and also you need to change lot .and your button click should be need write to
您已将值推送到错误的 object 。而且你需要改变很多。你的按钮点击应该需要写入
$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 = "";
}
});