gpt4 book ai didi

javascript - 选择多个项目,而不仅仅是一个(angularJs)

转载 作者:行者123 更新时间:2023-11-30 16:15:35 25 4
gpt4 key购买 nike

我在 div 中显示了产品,用户可以点击它们来选择它们。我遇到的问题是只能选择一种产品。我不想将此功能更改为多选功能,并且该功能应该收集产品的 ID。

这是我的产品 HTML

<div class="product_item hit w_xs_full" ng-repeat="p in products track by $index" style="float: left; background-color: blue;">
<figure class="r_corners photoframe type_2 t_align_c tr_all_hover shadow relative"
ng-click="selectUserItems($index)" ng-class="{sel: $index == selected}">
<!--product preview-->
<img ng-src="images/products/{{p.images[0].thumbName}}" alt="{{p.name}}" class="tr_all_hover">
<!--description and price of product-->
<figcaption>
<br>
<h5 class="m_bottom_10"><b>{{p.name}}</b></h5>
<p class="scheme_color f_size_large m_bottom_15" style="color:black;">Located in: <b>{{p.country}}</b></p>
<a href="#/swap/{{p.mainCategorieLink}}/{{p.subCategoryLink}}/{{p.alias}}">
<button class="button_type_4 bg_scheme_color r_corners tr_all_hover color_light mw_0 m_bottom_15">See item</button>
</a>
</figcaption>
</figure>
</div>

这是我选择项目的 Controller 功能

//select items
$scope.selectUserItems = function(index){
$scope.selected = index;
};

一旦项目被选中,div 的背景就会变成蓝色

.sel {
background-color:#5ACBFF;
}

那么如何正确编写 Controller 函数,以便您可以选择多个 div 和 $scope.selectd 变量收集产品的 ID。

最佳答案

目前,$scope.selected = index; 仅包含一个索引,您需要将其设为数组并切换当前选择:

变体 1

 $scope.selected[index] = !$scope.selected[index];

并更改您的 ngClass 定义:

 ng-class="{sel: $index[selected]}"

变体 2

或更改您的ngClick-函数:

 ng-click="selectUserItems(p)"

selectUserItems 函数

 $scope.selectUserItems = function(item){
item.selected = !item.selected;
};

并更改您的 ngClass 定义:

 ng-class="{sel: p.selected}"

关于javascript - 选择多个项目,而不仅仅是一个(angularJs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35571819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com