<div>就像Radio Button一样

时间:2015-10-09 09:32:38

标签: angularjs angularjs-ng-repeat toggle

如何让它在ng-repeat中只选择/取消选择一个元素? 到目前为止,我有这个代码,但通过这种方式,我可以选择任何元素,而不仅仅是一个:

let texts = Int(number1Field.text?)

4 个答案:

答案 0 :(得分:3)

请检查:JSFiddle

<ul>
  <li data-ng-repeat="image in images">
    <div data-ng-class="{'selected':  isSelected == $index}" data-ng-click="updateSelected($index)">a</div>
  </li>
</ul>

答案 1 :(得分:0)

试试这个 -

<div data-ng-repeat="images in list">
<div data-ng-class="{'col-sm-12' : isImageSelected, 'col-sm-9' : !isImageSelected}" data-ng-click="isImageSelected = !isImageSelected">
    something
</div>

答案 2 :(得分:0)

You need keys to add class to specific element in the ng-repeat, so basically the snippet will look like this:

<div data-ng-repeat="( key,image ) in list">
    <div data-ng-class="activeImage === key ? 'selected' : ''" data-ng-click="activeImage = key">
        something
    </div>
</div>

答案 3 :(得分:0)

我用按钮和数字回复了这个简单的代码,它对我有用:

<div ng-repeat="(key, number) in myCtrl.numbers">
    <button class="btn " ng-class="isImageSelected ? 'btn-success' : ''" ng-init="isImageSelected = false" ng-click="isImageSelected = !isImageSelected">
        {{number}}
    </button>
</div>

在我的控制器中,我有vm.numbers = [1, 2, 3, 4, 5]

这里ng-init在每次ng-repeat迭代的范围内创建不同的变量,并且不会调用与代码相同的变量。你不应该像文档https://docs.angularjs.org/api/ng/directive/ngInit

那样滥用ng-init