Implementing multiple ng-hide directives in Angular with a single shared variable

I've hit a roadblock with something that should be simple. I was planning on creating a factory to tackle this issue, but then I realized there might be a more elegant solution.

In my directive templateUrl, I have an ng-repeat:

<div ng-repeat="item in items">
   <p ng-click="toggle()" ng-hide="display">{{item.$value}}</p>
   <input ng-model="item.$value" ng-show="display">
   <button ng-show="display" ng-click="changeText(item)">finish</button>
</div>

This is what my toggle function looks like:

$scope.toggle = function (){
      $scope.display = !$scope.display;
    };  

So, when I click on one <p> element, all input fields are displayed because they use the same variable. I'm struggling to find a proper angular solution that would only toggle the field within the item itself. Is there a solution out there for this dilemma?

Answer №1

To improve functionality, consider utilizing item.display instead of simply display. Ensure to pass item in the toggle() function. Here's an example implementation:

<div ng-repeat="item in items">
    <p ng-click="toggle(item)" ng-hide="item.display">{{item.$value}}</p>
    <input ng-model="item.$value" ng-show="item.display">
    <button ng-show="item.display" ng-click="changeText(item)">finish</button>
</div>

JavaScript logic:

$scope.toggle = function (item) {
    item.display = !item.display;
};

Answer №2

Give this a shot:

<div ng-repeat="element in elements">
     <p ng-click="toggle(element)" ng-hide="element.show">{{element.$value}}</p>
     <input ng-model="element.$value" ng-show="element.show">
     <button ng-show="element.show" ng-click="updateText(element)">complete</button>
</div>

$scope.toggle = function (element){
  element.show = element.show || false;
  element.show = !element.show;
}; 

By storing the "show" property within each element, you can individually toggle it for every item. If an element's "show" property is undefined, it will default to "false".

This is just one way of handling it, there could be more optimal solutions out there.

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

`vuetify sidebar with a nested menu``

I am trying to enhance the vuetify sidebar menu I created by adding a submenu to a specific title. Despite editing the code and data as shown below, I am unable to see any changes reflected. Below is the code snippet: <v-list flat class="mt- ...

"Utilizing AngularJS: Incorporating multiple ng-views within a single template

While developing a dynamic web application with AngularJS, I came across a question - is it feasible to include multiple ng-view elements within a single template? ...

Guide on leveraging a private GitHub repository as a yarn dependency without installing internal dependencies

Recently, I have been attempting to incorporate a private GitHub repository as a dependency within multiple repositories at my workplace. Within the primary package.json file, our dependency is outlined as follows: "mycompany-models": "https://github.com ...

Utilize the controller's scope within a different controller

Hello, I am a beginner with Angular and I have a question. How can I access the scope of one controller from another? I would appreciate any simple and efficient methods to achieve this. Thank you in advance. <body ng-app="myApp"> <div ng-c ...

Guide to Positioning Pre-Loader in the Center on Mobile Devices

Despite trying numerous solutions, I'm struggling to center my pre-loader on mobile devices. It looks fine on desktop and mobile landscape orientations, but whenever I switch to mobile portrait, it just won't center properly. CSS: .load ...

Enhancing Navigation in React Native by Passing Multiple Values to the Next Screen

I have a challenge where I can successfully pass the first value of (postONE) from the current screen to Screen Two, and it displays perfectly there. However, I am unable to show the second value of (postTwo). This means that the Next screen only shows the ...

Problem with Ionic Material's item class

Currently, I am working on the Ionic material demo app and encountering an issue. When I do not use the "item class," everything works fine, but the UI does not appear as expected because that class is missing. The code is as follows: <div class ...

Execute two operations using jQuery and PHP

I recently created a form for users to fill out, with the data being sent to my email. However, I also wanted the email information to be posted to my MailChimp account using their API. Unfortunately, this feature is currently not functioning correctly. B ...

Can you please direct me to the location of the "Vue starting point"?

Currently, I am in the process of installing bootstrap-vue into my vue-cli project. I referred to the documentation provided at for guidance: To begin, BootstrapVue needs to be registered in your app entry point: import Vue from 'vue' import ...

Use the date-fns max function to identify the latest date in an array of date strings

The Dates string array is created using data from the backend object in the following manner: const dates: string[] = this.data.map((item:any) => item.date.jsdate); Here is the resulting array: dates = [ Thu Jan 12 2015 00:00:00 GMT+0530 (India T ...

Tips for properly showcasing images on a webpage after ensuring all other content has loaded

Is there a way to have an image load last on a webpage after all other content has been loaded? I have an image that is retrieved from a database when a button is pressed, but I would prefer for the entire page to load first and then display the image. C ...

Exploring the Power of Arrays, For-Loops, and Functions in JavaScript

Currently, I have a functional code implementation, but I'm struggling with creating a proper loop. I'm seeking a solution to develop a function that changes the URL source of #mainImage to the one in the imgUrl array when hovering over an #id i ...

Retrieve the class using a text string and then obtain the subsequent string

I am searching for the first class containing the text "Routed:" and then I want to retrieve the following string. I attempted to utilize document.querySelector, but I'm uncertain if this is the correct approach. <td class="msvb2">Routed:</t ...

The CSS transition effect is smooth when the accordion opens, but it does not have the same effect when

Currently, I am creating an accordion component that is supposed to have a smooth transition effect when opened and closed. However, while the transition works smoothly when the accordion opens, it seems to have some issues when it comes to closing. I hav ...

Creating a Login Form with Node.js and MongoDB

Currently, I am working on a node.js application that is connected to a remote mongoDB server. Inside the database are specific custom codes that have been created and shared with selected users. The main objective is to restrict access to the rest of the ...

Does Somebody Possess a Fine Floating Tag?

Some time ago, I came across this floating label that I have been searching for ever since. I experimented with a few that appeared promising and initially worked well, but they ended up presenting their own issues. Some required the mandatory attribute f ...

Tips for enabling mouse wheel zoom on a three.js scene

I have a straightforward three.js graphic that I wanted to make zoomable by using the mouse wheel. I attempted to implement solutions from this and this question in order to achieve this feature. Ideally, I want users to be able to zoom in or out of the gr ...

Transferring data seamlessly between AJAX and PHP

<?php //Establishing site root variable; ?> <?php require_once("../includes/site_init.php"); ?> <!DOCTYPE HTML> <html lang = "en"> <head> <meta charset = "UTF-8"> <meta name = "viewport" content = "width ...

Using VueJs to invoke a plugin from a .js file

I'm seeking a deeper understanding of working with vueJS. My current setup In my Login.vue component, there is a function logUser() from UserActions.js which in turn calls the postRequest() function from AxiosFacade.js Additionally, I use a plugin ...

Guide to creating a Chrome extension that can detect updates in MongoDB documents

I have developed a chrome extension for syncing links, which stores the data in a MongoDB database using a custom REST API. While I am successfully able to push changes to the database and listen to them using a change stream in the REST API, I am facing d ...