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