Use ng-click to enable specific actions only on the element in which it is placed

Using ng-repeat, I have created a series of elements...

<div class="form-block" ng-repeat="form in formblock | filter:dateFilter">

    <div ng-click="showResults()" ng-if="repeat == true" class="drop">{{ form.form_name }} <span class="caret"></span></div>

    <div ng-show="results" class="formURL">{{ form.url }}</div>
    <div ng-show="results" class="formCount">{{ form.count }}</div>
    <div ng-show="results" class="formSubmit">{{ form.submit }}</div>

</div>

The ng-click="showResults()" function is responsible for toggling the display of other elements. However, I am facing an issue where I want this click event to only affect elements within the same container, not all elements.

In essence, I wish for the click event to exclusively impact elements within the container where the function is called. How can I achieve this?

This is how showResults function looks like in my controller...

$scope.showResults = function(){
  return ($scope.results ? $scope.results=false : $scope.results=true)
}

Answer №1

ng-repeat offers a unique variable (unless already defined): $index.

With this, you can keep an object $index => toggleState instead of a single boolean value in your AngularJS code:

$scope.hiddenItems = {};

$scope.toggleItem = function (idx) {
    $scope.hiddenItems[idx] = !$scope.hiddenItems[idx];
}

And in your HTML markup:

<div ng-repeat="item in items">
  <div class="item" ng-hide="hiddenItems[$index]">
  <h1>
  {{item}}
  </h1>
  Everything you need to know about {{item}}!
  <br />
  </div>
  <a ng-click="toggleItem($index)">Toggle {{item}}</a>
</div>

Check it out live on jsfiddle.net!

Answer №2

In order to display specific results based on index values, you can utilize the $index variable within your AngularJS code:

<div ng-click="showResults($index)" ng-if="repeat == true" class="drop">{{ form.form_name }} <span class="caret"></span></div>
<div ng-show="results[$index]" class="formURL">{{ form.url }}</div>
<div ng-show="results[$index]" class="formCount">{{ form.count }}</div>
<div ng-show="results[$index]" class="formSubmit">{{ form.submit }}</div>

Here is an example of how you can define the function for handling these results:

$scope.showResults = function(index){
  return ($scope.results[index] ? $scope.results[index]=false : $scope.results[index]=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

Choose the appropriate data type for the class variable (for example, fArr = Uint32Array)

const functionArray: Function = Uint32Array; new fArr(5); The code snippet above is functioning properly. However, TypeScript is throwing a TS2351 error: "This expression is not constructable. Type 'Function' has no construct signatures". I wo ...

Implementing a timed delay before assigning a class in the state

I am trying to implement a delay before applying for a new class. This is my current situation const [isDone, setIsDone] = useState<boolean>(false); Within a method, I have the following code snippet const myMethod = () => { .... .... se ...

Tips for refreshing the current page with passing a parameter in the URL?

To achieve the functionality of reloading the page whenever different buttons are pressed and sending a String to the same page, I need to take that String on created() and use it to send an HTTP Get into my Database. My current setup looks like this: exp ...

Experimenting with mocha and Nightmare.js, utilizing traditional syntax and without the use of ES6 features

Here is a real-world example that I am using: How to Use Nightmare.js without ES6 Syntax and Yield However, when I include it in a Mocha test, it times out. Here's the code snippet: describe('Google', function() { it('should perform ...

Issue: The content of the text does not align with the HTML generated by the server

I'm struggling with an algorithm in next.js and encountering hydration errors. Here is the code I am using: import numbers from "../../functions/numberGenerators.js" export default function test() { ...

Unable to update the information within a DIV using Python Django, as well as the webpage following a jQuery action

Exploring new ideas through the lens of two variables in an HTML template, messages and users, a series of buttons trigger a jQuery function upon being clicked. This function sends a post request to a Django server, which then returns an update to the mess ...

Display the keys of a nested array in Vue.js when the structure is unknown

Here is a representation of a dynamic array I have: nodes: [ { n1: "Foods", }, { n4: "Drinks", b7: [ { a2: "Beers", a4: [ ...

Navigate within a div using arrow keys to reposition another div

As a newcomer to JavaScript, I am facing some challenges. My goal is to use arrow keys to move a small div inside a larger div. However, the code below is not functioning as expected. Here is the HTML and CSS: <div id="rectangle"> <div id="s ...

Scrolling seamlessly within a container that is fixed in position

For hours, I've been attempting to incorporate smooth scrolling into my project with no success. The issue lies in the container where the anchor tags are located. If it's set to fixed position or absolute, none of my attempts seem to work. In ...

Is there a way to stop my <pre> code from being displayed on the page?

Currently, I am utilizing the google code prettifier found at http://code.google.com/p/google-code-prettify/ This tool functions similarly to stack overflow by enhancing and highlighting syntax when a block of code is input. However, I have encountered a ...

Uploading a Node.js Package to GitHub Packages - Issue ENEEDAUTH

Hello everyone, I am currently attempting to deploy my NPM package to GitHub packages using the following yaml configuration: # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created # For m ...

Using the ng-class directive, you can dynamically apply a specific class based on the value

I utilized angular to display content based on selected values from a dropdown. Now, I am looking to dynamically add a class to a div when specific values are chosen. Essentially, I want to apply certain CSS classes to a div depending on the selection mad ...

yo projectname Angular Command

Every time I run this command, I encounter a new error. It seems like as soon as I fix one module issue, another pops up. For instance, I recently encountered an error with the 'shelljs' module. The specific error message is as follows: Error: ...

How can I show the initial three digits and last three digits when using ngFor loop in Angular?

Greetings! I have a list of numbers as shown below: array = [1,2,3,4,5,6,7,8,9,10] By using *ngFor, I am displaying the numbers like this: <div *ngFor =" let data of array"> <p>{{data}}</p> </div> Now, instead of d ...

Deactivate a setInterval function within a Vue.js component

I am facing an issue in my VUE SPA where I have a component running a recursive countdown using setInterval functions. The problem is that the countdown continues running in the background even when I switch to another view, but I want to destroy the setIn ...

Customized settings saved in local storage using JavaScript

Here is the script I am currently using for my app: <script> if (localStorage.getItem("0") === null) { //do nothing } else if(localStorage.getItem("1")===null{ } else if(localStorage.getItem("2")===null{ } else if(localStorage.getItem("3")===null ...

implementing lazy loading in AngularJS uib-tab

I am facing an issue with a uib-tab that has a heading and an ng-click method that toggles a boolean variable to load the content. The code is as follows: <uib-tab class="height-full"> <uib-tab-heading ng-click="vm.loadTab(2)"> ...

The AngularJs filter seems to be malfunctioning

I urge you to take a look at the following link: http://plnkr.co/edit/Cn0sNDGEkPOzV19ye8NW?p=preview I recently made changes to my JSON data, and now I am having trouble filtering it. var result = $filter('filter')(foo.results, {id:2426})[ ...

Having trouble retrieving data from the table with AJAX and CodeIgniter

I am currently developing a comprehensive HRM+CRM system (Human Resource Management and Customer Relation Management). I have encountered an issue while trying to generate an invoice for each customer. I am struggling to resolve this problem and would appr ...

403 Malicious Path Middleware Error in Express.js

Encountering an error when sending a post request to my server, but only on the production server - whereas the staging server is functioning properly. Both servers are hosted on AWS Ubuntu instances. Investigating the stack trace, it appears that the err ...