1

好的,这是泡菜:

我使用 ng-repeat 遍历菜单项:

<!-- 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
    }
}];

提前感谢您的任何见解。

4

1 回答 1

0

我不确定我是否正确理解了你的问题。

但是假设这menuItems.items是一个产品数组,由于某种原因,它的项目没有该属性qty,并且这theCart是一个不同的产品数组,其中的项目确实具有该属性qty。而您想要的是从数组中获取您正在迭代的产品的可用数量theCart,您可以使用过滤器 '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>

我必须说这是一种非常丑陋的方式,我不建议任何人这样做。在控制器中生成这两个数组的“连接数组”并迭代那个“连接数组”会好得多

于 2014-09-05T04:31:52.647 回答