0
我刚开始学习AngularJS,并试图创建一个简单的“商店”网络应用程序。这里是我的index.html:查找阵列中的对象
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<link type="text/css" rel="stylesheet" href="stylesheet.css" />
</head>
<body ng-app="myApp" ng-controller="store">
<div>
Remaining money:
{{money|currency}}
</div>
<div>
<table>
<tr>
<td><em>Item</em></td>
<td> </td>
<td><em>Price</em></td>
</tr>
<tr ng-repeat="item in store">
<td><b>{{item.Name}}</b></td>
<td> </td>
<td>{{item.Price|currency}}</td>
<td><button ng-click="buy(item)">Buy</button></td>
</tr>
</table>
</div>
<div>
<table ng-hide="stock.length===0">
<tr>
<td><em>Item</em></td>
<td> </td>
<td><em>Quantity</em></td>
</tr>
<tr ng-repeat="item in stock">
<td><b>{{item.Name}}</b></td>
<td> </td>
<td>{{item.Quantity}}</td>
</tr>
</table>
</div>
<script src="app.js"></script>
</body>
</html>
这是我的app.js:
var app = angular.module('myApp', []);
app.controller('store', function ($scope, $interval) {
$scope.money = 500;
$scope.store = [{
Name: "Cookie",
Price: 10
}, {
Name: "Banana Bread",
Price: 50
}];
$scope.stock = [];
$scope.buy = function (item) {
if (stock.indexOf(item) === -1) {
$scope.stock.push(item);
}
$scope.stock.item.Quantity += 1;
$scope.money -= $scope.store.item.Price;
};
});
问题在于团购功能。它从不将物品推到库存阵列上。由于我很新,我可能错过了一些非常简单的东西,但我无法弄清楚。
这样做了!谢谢。但现在它显示出来了,我发现我的对象属性'Quantity'没有被添加。有什么帮助吗? – WorkForPizza 2015-04-05 02:54:10
$ scope.stock.item.Quantity + = 1;没有物品存货。 – 2015-04-05 02:55:27
$ scope.stock.item.Quantity + = 1;代表 'stock = {item:{Quantity:0}}' – 2015-04-05 02:55:47