The $index can be effectively utilized to alternate the click event of elements nested within the same parent element

I'm currently utilizing ng-repeat to exhibit a multidimensional array...

<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>

The stated code presents numerous lists, with the title of each list denoted as {formblock[0].form_name}. Upon clicking this title, the aim is to toggle the visibility of each corresponding formblock, while ensuring the title remains visible.

Currently, the functionality does not operate as desired. Upon click, only elements from within formblock are hidden, specifically form. Additionally, this hiding action applies universally across all lists, whereas I intend for the ng-click function to exclusively toggle elements within the same container.

How can I accomplish this?

Here lies my controller function for ng-click:

$scope.showResults = function (idx) {
    $scope.results[idx] = !$scope.results[idx];
}

Answer №1

To utilize the item from ng-repeat in a function, you can follow this approach:

<div ng-repeat"item in list">
    <a href ng-click="doSomethingWith( item )">...</a>
</div>

$scope.doSomethingWith = function ( item ) {
    console.log('Fantastic! The item is:', item);
};

For your specific scenario:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The code within $viewContentLoaded runs prior to initializing the root scope

There is an issue with the asynchronous firing of the method on $viewcontentloaded. My problem involves a variable in the root scope, specifically in my Main controller, that needs to be initialized before the view controller loads. During module.run, I am ...

Working with the visibility of a button using JavaScript

My goal is to toggle the visibility of a button using JavaScript. Initially, on page load, the button should be hidden. function hideButton(){ var x = document.getElementById('myDIV'); x.style.display = 'none'; } The visibilit ...

Tips for managing an outdated AngularJS 1.5.5 application

Currently tasked with maintaining an outdated AngularJS 1.5.5 application, I am unsure how to properly set up the environment for it. What specific versions of NodeJS/NPM do I need to install in order for the gulp tasks to function correctly? Is there an ...

Steps for implementing remote modals in Bootstrap 5 using CS HTML

I'm attempting to implement a remote modal window in Bootstrap 5 with C# MVC. The endpoint for the modal partial view is configured correctly. According to this discussion on Github, all that needs to be done is to call some JavaScript. However, it ...

What is the best way to eliminate the additional slash from an optional parameter at the first level in a URL

Is it possible to make the first parameter optional for the login state? Below is the .state definition that I am using: $stateProvider .state("login", { url: '/:team/login/', templateUrl: 'app/user/login.html', ...

What is the best way to access automatically generated JavaScript variables in TypeScript?

I am currently facing an issue with integrating a Google login API in my React project and I need some help. The problem arises when the user already has an active session, rendering the API unnecessary. The Javascript solution provided is as follows: co ...

The issue of basic authentication failing to function on Internet Explorer and Chrome, yet operating successfully on Firefox

Here is how my authentication code looks: public class BasicAuthenticationMessageHandler : DelegatingHandler { private const string BasicAuthResponseHeader = "WWW-Authenticate"; private const string BasicAuthResponseHeaderValue = "Basi ...

What is the proper method for invoking object (class) methods from a router?

My apologies for the vague title. Let me clarify what I am attempting to accomplish. In this scenario, there are two main components: A class called 'wallet.js' A router named 'index.js' which handles GET requests This is my objectiv ...

Issue encountered when attempting to remove an element from an array using the delete method

Whenever I attempt to remove an input that has been added, an error message saying "Value is NULL" pops up. I'm looking for a solution where only the selected inputs are deleted when I click on DELETE, instead of all inputs being removed. The myFuncti ...

The development of the React app will pause momentarily upon encountering a single low-severity vulnerability. To address this, you can either run `npm audit fix` to resolve it, or use `npm audit` to gain

A challenge arises while executing the command below in Visual Studio Code to develop a react app: create-react-app my-first-react-app The process halts and displays this message: found 1 low severity vulnerability run `npm audit fix` to rectify th ...

Utilizing CSS-in-JS to eliminate arrow buttons from a TextField

I'm currently developing a webpage using React and Material-UI. One of the components I have is a TextField element, and I need to remove the arrow buttons that typically appear on number input fields. If I were utilizing CSS, I could easily achieve t ...

When using jQuery to select elements of a specific class, make sure to exclude the element that triggered the

A dynamic number of divs are generated from a data source. Each div contains an image button and another div with text. While the actual scenario is more complex, we can present a simplified version: <div id="main"> <div id="content_block_1" ...

Error: The function getAuth has not been defined - Firebase

I have included the code snippets from index.html and index.js for reference. The analytics functionality seems to be working fine, but I am facing an issue with authentication due to an error during testing. Thank you for your help. index.html <script ...

Ways to determine if the navigation bar is in a collapsed state or not

I am working with a navbar: <nav id="navbar" class="navbar fixed-top navbar-expand-md navbar-light bg-light"> <a class="navbar-brand" href="#"></a> <button id="toggle" class="n ...

"Using RxJS 6 for a countdown timer in an Angular 6 application

Currently, I am working on implementing a countdown timer in Angular 6 using RxJS 6. My goal is to have the ability to subscribe to the results and reset the timer as needed: Here is what I have tried so far: const timer = interval(1000).pipe( take(4) ); ...

Comparing the start and end times in tabular or array input using Yii2

Can I compare the start and end times in the Yii2 client/ajax validation for my form? https://i.sstatic.net/bC48T.png This is how my view file code looks: <?php foreach ($model->weekDaysList as $index => $value) : ?> <div class="row"& ...

The scrolling navigation bar appears behind the slider, causing the parallax effect on the slider to not work properly

While I was scrolling down, my menubar went behind the cycle slider. I tried using z-index 1, but it's not working. Can anyone provide a solution? I'm not sure what I did wrong. </div> <img src="http://malsup.github.io/imag ...

Unable to view sidebar navigation on the screen

I've been experimenting with the sidebar navigation from w3 schools, specifically trying to create a side-nav that opens from one div. You can see an example here: http://www.w3schools.com/w3css/tryit.aspfilename=tryw3css_sidenav_left_right&stack ...

Shuffling decks of cards among players at a table using JavaScript

Seems like such a simple task, but I can't seem to get it right. The idea is to have an array of 8 other arrays, each containing sets of cards (although in this case, they just have random numbers). Depending on whether passDirection is set to -1 or 1 ...

What is the timing for the execution of top-level non-export code in TypeScript?

I am currently puzzled about the execution of code in files. Let's say we have a file1.ts with the following content: export interface myInterface {} export function myFunction() {} export const myConst: {} // ... and more exports // top-level non- ...