Conceal this element within the ng-repeat loop

Is there a way to hide a div containing values within it when a button is clicked?

<div ng-repeat="item in items">
  <p>{{item.name}}</p>
  <div>{{item.comment}}</div>
  <div id="button">show comment</div>
</div>

Answer №1

<div ng-repeat="element in elements">
    <p>{{element.title}}</p>
    <div ng-show="element.visible">{{element.content}}</div>
    <div ng-hide="element.visible" ng-click="element.visible = true">show content</div>
    <div ng-show="element.visible" ng-click="element.visible = false">hide content</div>
</div>

We utilize a dynamic visible attribute within your Element object (not initially present, but generated within the ng-repeat loop) and set its value:

  • to false for initial hidden state.

or

  • to true for initial visible state.

By default, it is set to false.

Answer №2

Implement the use of ngShow directive in conjunction with the ngClick event to dynamically change an attribute (such as item.show in this scenario) of your item.

var app = angular.module('myApp', []);

app.controller('myController', ['$scope', function($scope){

  $scope.items = [{name:'name01', comment:'comment01'}, {name:'name02', comment:'comment02'}];

}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
  <div ng-repeat="item in items" >
    <p>{{item.name}}</p>
    <div ng-show="item.show">{{item.comment}}</div>
    <div id="button" ng-click="item.show = !item.show;">show comment</div>
  </div>
</div>

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

Divide the string into a collection of individual elements

Recently, I received a string from the backend that looks like this: var string = "{name: Hello, age: 20}, {name: Nadia, age: 30}". To transform it into an object, I attempted to push it into an empty array using the following code: var array = [ ...

Vue.js - Duplicate Navigation Error When Redirected to a New Page

I'm facing an issue with Vue.js and the NavigationDuplicated error. As I work on a web application that involves navigation across various pages, I encounter the "NavigationDuplicated" error unexpectedly. Even though I'm not attempting to naviga ...

The execution of the return statement in the catch block is unsuccessful

Here is a simple example that results in an error because the variable tl was not specified: function allmatches() { SpreadsheetApp.getActive().getSheetByName('data').getRange('A1').setValue(tl) } To track any errors that occur durin ...

Creating a form in NextJS to securely transfer user input data to MongoDB

Being new to JavaScript, I have a basic understanding and some lack of experience, but I am eager to learn more. Recently, I embarked on a project using NextJS, an efficient framework that integrates with ReactJS. My current challenge lies in creating a si ...

What steps can I take to stop my React child component from fetching data each time it re-renders or mounts?

Before delving into the code, let me provide a brief overview of the question at hand. https://i.sstatic.net/oRc7m.png The image above showcases a work in progress Next.js application that displays the status of my Philips Hue lights by interacting with ...

The Elusive Solution: Why jQuery's .css() Method Fails

I am currently facing an issue with my code that utilizes the jQuery .css() method to modify the style of a specific DIV. Unfortunately, this approach does not work as expected. To illustrate the problem, I have provided a simplified version of my code bel ...

Challenges with transitioning to IE 11

During the process of moving our application from IE 8 to IE 11, we encountered an unexpected problem. The following jQuery code that functioned properly in IE8 is failing to work in IE11. $("#Submit").attr("disabled", "disabled"); Is there anyone who ca ...

JavaScript change the object into a string

I've been working on code to convert strings into dictionaries and arrays, and vice versa. While string to array and string to object conversions are successful, the reverse process is giving me trouble. I'm currently stuck and unsure of how to r ...

Unable to locate request object during post request

I've created a pug form with the following structure: extends layout block content h1 David A Hines h2 #{posts[0].title} p #{posts[0].body} div form(action='/insert_post',method='post') div(id='title_div' ...

The mistake occurs when attempting to access a class property generated by a class constructor, resulting in a type error due to reading properties of

I'm having trouble building an Express API in TypeScript using Node.js. I am new to Express and I have been learning Node, JavaScript, and TypeScript since 2022, so I apologize if the question is not too complex. The issue I'm facing is trying to ...

JavaScript: Grab a single numerical value from a key-value array and save it to a variable

After executing an InfluxDB query, I receive a single result row in the following format: {"result":[[{"result":"_result","table":0,"_start":"2022-01-28T09:00:12.676771291Z","_stop":&quo ...

Hunting for an undetected issue within a promise

Currently developing a Firefox extension to scrape specific data from a website. The website features an index page that lists links to subsidiary pages containing the desired data. Upon visiting the index page, the extension prompts to scrape the data. I ...

Implementing Twain functionality in an Electron-based desktop application

I've been tasked with building a desktop application using React + Electron, and my client wants to incorporate scanning documents using a scanner and uploading them to the server through the app. Are there any effective ways to integrate Twain into R ...

Error message stating that the callback function was not triggered by the injected JSONP script in Angular

Currently, I am running an application on localhost://3000 using npm server. Here is the content of my services file: import {Injectable} from "@angular/core"; import {Jsonp} from "@angular/http"; import 'rxjs/add/operator/map'; @Injectable() ...

Making adjustments to a Jquery data attribute can have a direct impact on CSS

My CSS is using a data attribute, but when I try to change it with jQuery, the change is not reflected on the screen or in the DOM. $('#new-1').data('count', 5); .fa-stack[data-count]:after { position: absolute; right: -20%; to ...

If you encounter an unrecognized operator in Javascript, make sure to handle this error and return

I have a script that identifies operators in an array and uses them to calculate based on another array. Below is the script: function interpret(...args) { let operators = args[1]; //access the operators array let values = args[2] //numbers except t ...

jQuery is not updating the div as expected

While typing out this question, I'm hoping to uncover a solution that has eluded me so far. However, just in case... About a year ago, I successfully implemented something similar on another website and went through the code meticulously. Surprisingl ...

Utilizing Angular expressions within the formlyConfig.setType function allows for dynamic

An important issue arises: What could be the reason for an expression like class='{{prodStatusTextColor}}' not updating in the view even though the variable scope.prodStatusText is receiving new values? Background: In my application, a model is ...

What is the mechanism behind the functioning of StackOverflow's notification system?

Could you explain the technique that is utilized to transmit and receive data from the client to the server? How does it manage to provide almost real-time results when new changes take place? Can anyone demonstrate the code being used for this process? ...

Utilizing JavaScript to retrieve a JSON file from a GitHub Pages server

I'm currently working on my website hosted on GitHub pages, which only supports static websites but allows the use of JavaScript. I have a JSON file on the GitHub server that my website runs from and I need to fetch and process it using JavaScript. He ...