2017-07-24 26 views
0

我有一個購物車的Web應用程序,它使用AngularJS來添加一行,並在按'保存'按鈕時將整個表保存到本地存儲。我在另一個網站上找到了代碼,專注於做這件事(addRow部分),但它不適合我。有人有主意嗎?如果您需要AngularJS文件,我可以添加它。AngularJS addRow並保存

<!DOCTYPE html> 
<html lang="en" ng-app> 
<head> 
<meta charset="UTF-8"> 
<title>AngularJS Shopping Cart</title> 
<script src="js/angular10.js"></script> 
<script> 
    function CartController($scope) { 
     $scope.books =[ 
      {title: 'Absolute Java', 
       qty: 1, price: 114.95}, 
      {title: 'Pro HTML5', 
       qty: 1, price: 27.95}, 
      {title: 'Head First HTML5', 
       qty: 1, price: 27.89} 
     ]; 
     $scope.rows =[ 
      {title: 'New Book', qty: 1, price: 10.99} 
     ]; 
     $scope.removeBook = function(index) { 
      $scope.books.splice(index, 1); 
     } 
     $scope.addRow = function() { 
      $scope.rows.push($scope.rows); 
      } 
      } 
</script> 
<link rel="stylesheet" href="css/ex05.css"> 
</head> 
    <body ng-controller="CartController"> 
    <table> 
    <caption><b>My Books</b></caption> 
    <thead> 
     <tr> 
      <th>Title</th> 
      <th>Qty</th> 
      <th>UnitPrice</th> 
      <th>$UnitPrice</th> 
      <th>Line Total</th> 
      <th>Total </th> 
     </tr> 
    </thead> 
    <tbody ng-repeat="book in books"> 
     <tr> 
      <td> 
       <input ng-model="book.title" size="20"> 
      </td> 
      <td> 
       <input ng-model="book.qty" size="2"> 
      </td> 
      <td> 
       <input ng-model="book.price" size="8"> 
      </td> 
      <td>{{book.price | currency}}</td> 
      <td>{{book.price * book.qty | currency}}</td> 

      <td> 
       <button ng-click="removeBook($index)"> 
        Remove 
       </button> 
      </td> 
     </tr> 

    </tbody> 

</table> 
<div id ="update"> 
    <button id="insert" ng-click="addRow()"> 
      New 
     </button> 
    <button id="save" ng-click="save"> 
      Save 
     </button> 
    </div> 
    </body> 
</html> 
+0

究竟意味着不工作?推?您正在發送數組而不是內容.. –

回答

0

試穿一下你添加新行:

$scope.addRow = function() { 
    $scope.inserted = { 
    title: "", 
    qty: "", 
    price: "" 
    }; 
    $scope.books.push($scope.inserted); 
}; 
+0

該按鈕的HTML代碼應該不同嗎?或者我應該擺脫數組部分? – user121168

+0

我不認爲你可以指向正確的方向,從所有價格中獲得總額,所以無論何時添加新書,它都會更新。 – user121168

+0

@ user121168您的問題仍然存在?我會盡力給你一個答案。 –