1

好的,繼承人的泡菜:綁定值到對象屬性,在一個對象中,在陣列中,AngularJS

im使用納克 - 重複通過菜單項進行迭代:

<!-- start the list/loop --> 
<ion-list ng-repeat="this in menuItems.items track by $index" type="item-text-wrap"> 

    <a class="item" ng-click="addToCart({{this}})">{{this.name}} 
     <span class="badge badge-stable">{{theCart[$index].product.qty}}</span> 
    </a> 

</ion-list> 
<!-- end the list/loop --> 

問題出現當我嘗試從購物車'theCart [$ index] .product.qty'中的商品中獲取價值時,因爲$ index沒有綁定到任何特定商品,只是數組中的位置。我需要在數組中深入到一個唯一標識符2個對象,所以我可以確保使用Angular的雙向數據綁定來提供正確的值。

theCart: [{ 
    product: { 
     id: 1, 
     section: 'sides', 
     name: 'mayo', 
     price: 7, 
     outOfStock: '', 
     qty: 1 
    } 
}, { 
    product: { 
     id: 0, 
     section: 'sides', 
     name: 'ranch', 
     price: 6, 
     outOfStock: '', 
     qty: 1 
    } 
}]; 

在此先感謝您的任何見解。

+0

究竟是什麼問題? 「this.id」不起作用? – pixelbits 2014-09-05 04:13:35

回答

0

我不確定我是否正確理解了您的問題。

但假設menuItems.items是由於某種原因,其項目不具備的特性qty產品陣列和theCart是一個不同的陣列,其中的項目做有物業qty產品。而且,你想要的是讓你從陣列theCart迭代產品的可用數量,你可以使用filter 'filter',這樣做:

<ion-list ng-repeat="item in menuItems.items" type="item-text-wrap"> 
    <a class="item" ng-click="addToCart(item.id)">{{item.name}} 
     <span class="badge badge-stable"> 
      {{(theCart | filter:{product:{id:item.id} })[0].product.qty}} 
     </span> 
    </a> 
</ion-list> 

我必須說,這是一個非常醜陋的方式做到這一點,我不會推薦這樣做給任何人。 在控制器中生成這兩個數組的「連接數組」會更好,並且反覆使用「連接數組」來代替

+0

謝謝。我同意這看起來很醜陋。我想我只是將數組結構重構爲'cart:0'和'cart:1'布爾屬性的'menu []'數組。這看起來好像是以角度的方式,因爲它將以最蒸餾的形式利用雙向數據綁定。獲取結果的最小代碼。同意? – user3865639 2014-09-05 16:03:24

相關問題