Angularjs指令点击元素 [英] Angularjs directive clicking elements
问题描述
您好我想知道,当我有从纳克重复,要素我怎么能允许用户点击列表中的一个,并使其通过的.css强调()的列表。然后,如果用户点击另一个元素时,previous元件UN-亮点和新点击的元素亮点?
Hi I was wondering when I have a list of elements from a ng-repeat, how I can allow the user to click one of the list and make it highlight via .css(). Then if the user clicks another element, the previous element un-highlights and the newly-clicked element highlights?
感谢
推荐答案
您可以尝试使用NG单击设置所选项目和应用条件的CSS类纳克级:
You may try to use ng-click to set the selected item and apply a conditional css class with ng-class:
<ul>
<li ng-repeat="item in list" ng-class="{'highlight': model.selected == item}">
<button ng-click="model.selected = item">Select</button>
</li>
</ul>
在你的控制器:
$scope.model = { selected : null };
我们需要使用一个对象,范围为NG-重复的每一个项目共同举办所选的项目,否则就存在于每一个项目,因此多个项目可以选择。
We need to use an object to hold the selected item so the scope is shared for every items of the ng-repeat, otherwise, it would exist for every items, therefore multiple items could be selected.
DEMO: http://plnkr.co/编辑/ EdrD7MvesW3xn9C9c6fk?p = preVIEW
这篇关于Angularjs指令点击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!