2014-03-25 86 views
0

我試圖在產品列表的表單上實現遞增/遞減函數。這個想法是能夠通過產品數量實時更新購物車,從而將產品添加到購物車中。以角度形式遞增遞減量

我從Angular docs中找到了這個代碼示例,我認爲它可以做到這一點。 http://docs.angularjs.org/api/ng/directive/ngClick

我很期待,當我對產品點擊它首次增加了產品形成PARAMS然後增加每次我對產品的挖掘是(另一種解決方案是HTML5的風格號字段雖然但仍然發出表單域邏輯)

enter image description here

"order": [{"product_id": "2", "quantity": "10"}, {"product_id": "5", "quantity": "12"}]

我的問題是: - 我不能使用FORMDATA 形式的權利(產品*量) - Incremen t數量的功能是遙遠

Plunker http://plnkr.co/edit/lCZUXpTeJqGkOllXaS2t?p=preview

<form ng-submit="processForm()"> 
    <pre>{{ formData }}</pre> 

    <ul ng-repeat="product in filtered = (products | orderBy:['name'] | filter:q)"> 
     <li>{{ product.name }} 
     <a ng-click="count = count + 1" ng-init="count=0" ng-model="formData.products">Increment - count: {{count}} 
      <input type="number" ng-model="formData.{{product.id}}" /> 
    </a> 
    </li> 
    </ul> 
</form> 
+0

你可以提供一個plunker或給我們介紹一下什麼是你的問題更詳細? –

+0

@ wickY26問題是,與樣機相比,目前的形式還是比較落後的。將嘗試稍後提供plunker示例 – olimart

+0

@ wickY26添加Plunker鏈接http://plnkr.co/edit/lCZUXpTeJqGkOllXaS2t?p=preview – olimart

回答

1

你只能通過結合ng-model設置數量,但你正在尋找設置完整的對象{product_id: x, quantity: y} ...

所以用NG-變化,而不是NG-模型設置FORMDATA ...

HTML的

<input type="number" ng-model="quantity" placeholder="product_{{product.id}}" 
ng-change="changeQuantity($index, product.id, quantity)"> 

控制器

$scope.changeQuantity =function (index, productId, quantity) { 
    $scope.formData[index] = {product_id: productId, quantity: quantity}; 
}; 

這裏工作PLUNKER

+0

Thanks @ wickY26當數量爲零或0時,完成從數組中刪除產品的任何想法例如,當用戶輸入數量,然後決定刪除產品) – olimart

+0

@ olivier plunker更新檢查它... http://plnkr.co/edit/V1GEsL5IooR8MROuiAoc –

+0

謝謝我欣賞。轉換爲咖啡腳本時,會在更改數量時引發錯誤。 '錯誤:'undefined'不是一個函數(評估'$ scope.formData.push({product_id:productId, quantity:quantity })')' – olimart