![](/img/trans.png)
[英]How do I toggle a class on click of an element but remove the same class from all other elements using JQuery?
[英]how to use $index to toggle click event of elements within the same element
我正在使用ng-repeat
顯示多維數組...
<div class="form-container" ng-repeat="formblock in forms">
<div ng-click="showResults($index)" ng-if="repeat == true" class="drop" type="button">{{ formblock[0].form_name }}</div>
<div ng-hide="results[$index]" class="form-block" ng-repeat="form in formblock">
<div class="optionWrap">
<div class="formURL">{{ form.url }}</div>
<div class="formCount">{{ form.count }}</div>
<div class="formSubmit">{{ form.submit }}</div>
</div>
</div>
</div>
因此,上面的代碼將顯示許多列表,每個列表的標題為{formblock[0].form_name}
。 單擊此標題后,我想切換每個formblock
的顯示,但我想保持標題可見。
上面的代碼無法正常工作,當單擊時,僅隱藏了formblock
一個組件,即隱藏了form
。 此外,它對所有列表都隱藏了該列表,在這里我希望ng-click
僅切換與該函數位於同一容器內的元素。
我該如何實現?
這是我的ng-click
控制器功能
$scope.showResults = function (idx) {
$scope.results[idx] = !$scope.results[idx];
}
您可以將ng-repeat的項目傳遞給函數。 例如:
<div ng-repeat"item in list">
<a href ng-click="doSomethingWith( item )">...</a>
</div>
$scope.doSomethingWith = function ( item ) {
console.log('Fabulous! I received:', item);
};
在您的特定情況下:
<div class="form-container" ng-repeat="formblock in forms">
<div ng-click="showResults( formblock )" ng-if="repeat == true" class="drop" type="button">{{ formblock[0].form_name }}</div>
<div ng-hide="! formblock.showResults " class="form-block" ng-repeat="form in formblock">
...
</div>
</div>
$scope.showResults = function ( formBlock ) {
formBlock.showResults = true;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.